{"id":21974117,"url":"https://github.com/javaobjects/demo_spring05_di_annotation","last_synced_at":"2026-04-19T10:32:14.603Z","repository":{"id":105562383,"uuid":"192912055","full_name":"javaobjects/demo_spring05_di_annotation","owner":"javaobjects","description":null,"archived":false,"fork":false,"pushed_at":"2019-06-21T06:02:36.000Z","size":4689,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T23:27:46.398Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/javaobjects.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-20T11:59:40.000Z","updated_at":"2019-06-21T06:02:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"b0278aca-c7f2-45de-813f-1175bee6ac82","html_url":"https://github.com/javaobjects/demo_spring05_di_annotation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/javaobjects/demo_spring05_di_annotation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring05_di_annotation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring05_di_annotation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring05_di_annotation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring05_di_annotation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/javaobjects","download_url":"https://codeload.github.com/javaobjects/demo_spring05_di_annotation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring05_di_annotation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266221327,"owners_count":23894966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T15:38:10.763Z","updated_at":"2026-04-19T10:32:09.553Z","avatar_url":"https://github.com/javaobjects.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Java-Spring基于注解的方式给依赖属性赋值\n\n*沿用上一个项目*\n\n1. 注释掉属性的set/get方法，并运行测试，如图所示测试失败\n\n![](60-Images/1.png)\n\n**结论：** 根据类型自动装配还是需要配置属性的get/set方法\n\n2. 还原被注释掉的属性的set/get方法\n\n3. 新建一个Java Project命名为 demo_spring05_di_annotation\n\n4. 导入对应的jar包\n\n![](60-Images/2.png)\n\n5. 写对应层的类 以及配置beans.xml\n\n**MyController**\n\n```\npackage test;\n\npublic class MyController {\n\n\tprivate MyServiceIfac service;\n\t\n\tpublic MyController(MyServiceIfac myService)\n\t{\n\t\tthis.service = myService;\n\t}\n\tpublic void login() \n\t{\n\t\tSystem.out.println(\"MyController login........\");\n\t\tservice.serviceLogin();\n\t}\n}\n```\n\n**MyDao**\n\n```\npackage test;\n\npublic class MyDao implements MyDaoIfac {\n\n\t/* (non-Javadoc)  \n\t * \u003cp\u003eTitle: queryUserByNameAndPwd\u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e    \n\t * @see test.MyDaoIfac#queryUserByNameAndPwd()  \n\t */\n\t@Override\n\tpublic void queryUserByNameAndPwd() \n\t{\n\t\tSystem.out.println(\"MyDao queryUserByNameAndPwd\");\n\t}\n}\n```\n\n**MyDaoIfac**\n\n```\npackage test;\n\npublic interface MyDaoIfac {\n\n\tvoid queryUserByNameAndPwd();\n\n}\n```\n**MyService**\n\n```\npackage test;\n\npublic class MyService implements MyServiceIfac {\n\n\tprivate MyDaoIfac dao;//MyService依赖MyDao\n\t/**\n\t * 为咯给属性赋值 就在属性所在的\n\t * 类里面设置一个构造方法\n\t * 并且该构造方法接收一个该属性类开的参数\n\t * \u003cp\u003eTitle: \u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e  \n\t * @param myDao\n\t */\n\tpublic MyService(MyDaoIfac myDao)\n\t{\n\t\tthis.dao = myDao;\n\t}\n\t/* (non-Javadoc)  \n\t * \u003cp\u003eTitle: serviceLogin\u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e    \n\t * @see test.MyServiceIfac#serviceLogin()  \n\t */\n\t@Override\n\tpublic void serviceLogin() {\n\t\tSystem.out.println(\"MyService serviceLogin()......\");\n\t\tdao.queryUserByNameAndPwd();\n\t}\n}\n```\n**MyService**\n\n```\npackage test;\n\npublic class MyService implements MyServiceIfac {\n\n\tprivate MyDaoIfac dao;//MyService依赖MyDao\n\t/**\n\t * 为咯给属性赋值 就在属性所在的\n\t * 类里面设置一个构造方法\n\t * 并且该构造方法接收一个该属性类开的参数\n\t * \u003cp\u003eTitle: \u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e  \n\t * @param myDao\n\t */\n\tpublic MyService(MyDaoIfac myDao)\n\t{\n\t\tthis.dao = myDao;\n\t}\n\t/* (non-Javadoc)  \n\t * \u003cp\u003eTitle: serviceLogin\u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e    \n\t * @see test.MyServiceIfac#serviceLogin()  \n\t */\n\t@Override\n\tpublic void serviceLogin() {\n\t\tSystem.out.println(\"MyService serviceLogin()......\");\n\t\tdao.queryUserByNameAndPwd();\n\t}\n}\n```\n**MyServiceIfac**\n\n```\npackage test;\n\npublic interface MyServiceIfac {\n\n\tvoid serviceLogin();\n\n}\n```\n**Test测试类**\n\n```\npackage test;\n\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\npublic class Test {\n\n\tpublic static void main(String[] args) {\n\t\t//实例化spring容器及bean\n\t\tApplicationContext act = new ClassPathXmlApplicationContext(\"beans.xml\");\n//\t\t得到的是此处的controller\n//\t    \u003cbean id=\"controller\" class=\"test.MyController\"\u003e\n//\t\t\t\u003cconstructor-arg index=\"0\" ref=\"service\"\u003e\u003c/constructor-arg\u003e\n//        \u003c/bean\u003e\n\t\tMyController c = (MyController) act.getBean(\"controller\");\n\t\tc.login();\n\t}\n}\n```\n**beans.xml**\n\n```\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\n\u003cbeans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:tx=\"http://www.springframework.org/schema/tx\"\n       xmlns:context=\"http://www.springframework.org/schema/context\"\n       xsi:schemaLocation=\"http://www.springframework.org/schema/beans \n       http://www.springframework.org/schema/beans/spring-beans.xsd \n       http://www.springframework.org/schema/tx \n       http://www.springframework.org/schema/tx/spring-tx.xsd \n       http://www.springframework.org/schema/context \n       http://www.springframework.org/schema/context/spring-context.xsd\"\u003e\n  \u003c!--      先配置最底层的Dao --\u003e\n       \u003cbean id=\"dao\" class=\"test.MyDao\"\u003e\u003c/bean\u003e\n       \u003cbean id=\"service\" class=\"test.MyService\"\u003e\n\u003c!--        构造函数注入     constructor-arg:构造器-参数 --\u003e\n\t\t\t\u003cconstructor-arg index=\"0\" ref=\"dao\"\u003e\u003c/constructor-arg\u003e\n       \u003c/bean\u003e\n       \u003cbean id=\"controller\" class=\"test.MyController\"\u003e\n       \t\t\t\u003cconstructor-arg index=\"0\" ref=\"service\"\u003e\u003c/constructor-arg\u003e\n       \u003c/bean\u003e\n\u003c/beans\u003e\n```\n6. 详细配置\n\n+ 引入刚刚忘记引入的jar包\n\n![](60-Images/3.png)\n\n+ 为对应的类写入头（戴帽子）\n\n**MyController**\n\n```\npackage test;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Controller;\n\n@Controller\npublic class MyController {\n\n\t@Autowired//自动装配的意思 默认根据类型装配\n\tprivate MyServiceIfac service;\n\t\n\t/**\n\t *  如果有一个类不知道属于哪一层的话就写@Component 表示组件的意思 \n\t * \u003cp\u003eTitle: login\u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e\n\t */\n//\tpublic MyController(MyServiceIfac myService)\n//\t{\n//\t\tthis.service = myService;\n//\t}\n\tpublic void login() \n\t{\n\t\tSystem.out.println(\"MyController login........\");\n\t\tservice.serviceLogin();\n\t}\n}\n```\n\n**MyDao**\n\n```\npackage test;\n\nimport org.springframework.stereotype.Repository;\n\n//又有接口又有实现类 帽子给实现类带\n\n@Repository\npublic class MyDao implements MyDaoIfac {\n\n\t/* (non-Javadoc)  \n\t * \u003cp\u003eTitle: queryUserByNameAndPwd\u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e    \n\t * @see test.MyDaoIfac#queryUserByNameAndPwd()  \n\t */\n\t@Override\n\tpublic void queryUserByNameAndPwd() \n\t{\n\t\tSystem.out.println(\"MyDao queryUserByNameAndPwd\");\n\t}\n}\n```\n**MyDaoIfac**\n\n```\npackage test;\n\npublic interface MyDaoIfac {\n\n\tvoid queryUserByNameAndPwd();\n\n}\n```\n**MyService**\n\n```\npackage test;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;//这是一个注解\n\n@Service\npublic class MyService implements MyServiceIfac {\n\n\t@Autowired\n\tprivate MyDaoIfac dao;//MyService依赖MyDao\n\t/**\n\t * 为咯给属性赋值 就在属性所在的\n\t * 类里面设置一个构造方法\n\t * 并且该构造方法接收一个该属性类开的参数\n\t * \u003cp\u003eTitle: \u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e  \n\t * @param myDao\n\t */\n//\tpublic MyService(MyDaoIfac myDao)\n//\t{\n//\t\tthis.dao = myDao;\n//\t}\n\t/* (non-Javadoc)  \n\t * \u003cp\u003eTitle: serviceLogin\u003c/p\u003e  \n\t * \u003cp\u003eDescription: \u003c/p\u003e    \n\t * @see test.MyServiceIfac#serviceLogin()  \n\t */\n\t@Override\n\tpublic void serviceLogin() {\n\t\tSystem.out.println(\"MyService serviceLogin()......\");\n\t\tdao.queryUserByNameAndPwd();\n\t}\n}\n```\n\n**MyServiceIfac**\n\n```\npackage test;\n\npublic interface MyServiceIfac {\n\n\tvoid serviceLogin();\n\n}\n```\n\n**Test类**\n\n```\npackage test;\n\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\npublic class Test {\n\n\tpublic static void main(String[] args) {\n\t\t//实例化spring容器及bean\n\t\tApplicationContext act = new ClassPathXmlApplicationContext(\"beans.xml\");\n//\t\t得到的是此处的controller\n//\t    \u003cbean id=\"controller\" class=\"test.MyController\"\u003e\n//\t\t\t\u003cconstructor-arg index=\"0\" ref=\"service\"\u003e\u003c/constructor-arg\u003e\n//        \u003c/bean\u003e\n\t\tMyController c = (MyController) act.getBean(\"myController\");\n\t\tc.login();\n\t}\n}\n```\n**beans.xml**\n\n```\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\n\u003cbeans xmlns=\"http://www.springframework.org/schema/beans\"\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:tx=\"http://www.springframework.org/schema/tx\"\n       xmlns:context=\"http://www.springframework.org/schema/context\"\n       xsi:schemaLocation=\"http://www.springframework.org/schema/beans \n       http://www.springframework.org/schema/beans/spring-beans.xsd \n       http://www.springframework.org/schema/tx \n       http://www.springframework.org/schema/tx/spring-tx.xsd \n       http://www.springframework.org/schema/context \n       http://www.springframework.org/schema/context/spring-context.xsd\"\u003e\n       \u003c!-- \n       \t启动注解，全包扫描\n       \tbase-package=\"com.ptcs.demo.dao\"(基本包名 com.ptcs.demo)\n       \tbase-package=\"com.ptcs.demo.control\"\n        --\u003e\n       \u003ccontext:component-scan base-package=\"test\"\u003e\u003c/context:component-scan\u003e\n\u003c/beans\u003e\n```\n\n7. 测试;如图所示，测试成功;\n\n![](60-Images/4.png)\n\n**备注：** \n```\n每一个类至少要有一个空的构造方法\n\n使用注解的方式给依赖属性赋值非常方便：\n\n1.不需要在xml文件中注册bean了；\n2.不需要提供属性的setter/getter方法了；\n\n如何使用注解开发：\n\n1.导入aop相关的包\n\naop   aspect\n\n2.在xml文件中开启全包扫描：\n\u003c!-- 启动注解，全包扫描 --\u003e\n\t\u003ccontext:component-scan base-package=\"test\"\u003e\u003c/context:component-scan\u003e\n\n\t\n3.编写完bean的代码后，给bean带上帽子：@Service  @Controller  @Repository  @Component\n\n4.还要给依赖的属性带上帽子： @Autowired  @Resource  \n建议大家使用框架的时候后专心使用框架API，不要一边使用框架API，一边使用JavaAPI，所以使用@Autowired\n\n@Autowired 默认按类型装配，\n@Resource默认按名称装配，当找不到与名称匹配的bean才会按类型装配。\n```\n\n**以上就是我关于 Java-Spring基于注解的方式给依赖属性赋值 知识的整理与总结 另附上[源码地址](https://github.com/javaobjects/demo_spring05_di_annotation)**\n\n==================================================================\n#### 分割线\n==================================================================\n\n**博主为咯学编程：父母不同意学编程，现已断绝关系;恋人不同意学编程，现已分手;亲戚不同意学编程，现已断绝来往;老板不同意学编程,现已失业三十年。。。。。。如果此博文有帮到你欢迎打赏，金额不限。。。**\n\n![](https://upload-images.jianshu.io/upload_images/5227364-0824589594f944c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavaobjects%2Fdemo_spring05_di_annotation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjavaobjects%2Fdemo_spring05_di_annotation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavaobjects%2Fdemo_spring05_di_annotation/lists"}