Spring学习笔记(四)

    xiaoxiao2021-12-14  13

    1、注解注入

    注解注入顾名思义就是通过注解来实现注入。

    Spring和注入相关的常见注解有Autowired、Resource、Qualifier。

    Autowired,按类型装配。Resource,按名称装配,当找不到与名称匹配的bean才会按类型装配。Qualifier和Autowired配合使用,指定bean的名称。

    Autowired和Resource注解可以标注在属性或属性的Set方法上。

    为了让系统能够识别相应的注解,需要在配置文件中配置注册对注解进行解析处理的处理器。使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean。使用@Resource、@PostConstruct、@PreDestroy等注解,必须事先在Spring容器中声明CommonAnnotationBeanPostProcessor的Bean。Spring为我们提供了简化配置方式,<context:annotation-config/>,自动完成声明。

    2、实例

    (1)PersonDao.java

    public interface PersonDao { public void add(); }

    (2)PersonDaoBean.java

    public class PersonDaoBean implements PersonDao { public void add() { System.out.println("PersonDaoBean add"); } }

    (3)PersonService.java

    public interface PersonService { public void save(); }

    (4)PersonServiceBean1.java

    public class PersonServiceBean1 implements PersonService { @Autowired private PersonDao personDao; public PersonServiceBean1() {} public PersonServiceBean1(PersonDao personDao) { this.personDao = personDao; } public void save(){ personDao.add(); } }

    (5)PersonServiceBean2.java

    public class PersonServiceBean2 implements PersonService { private PersonDao personDao; @Autowired public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public PersonServiceBean2() {} public PersonServiceBean2(PersonDao personDao) { this.personDao = personDao; } public void save(){ personDao.add(); } }

    (6)PersonServiceBean3.java

    public class PersonServiceBean3 implements PersonService { /** * @Autowired(required=true)表示必须为personDao注入值 * @Qualifier("personDao")指定注入name为personDao的bean,如果不存在则异常 */ @Autowired(required=true) @Qualifier("personDao") private PersonDao personDao; public PersonServiceBean3() {} public PersonServiceBean3(PersonDao personDao) { this.personDao = personDao; } public void save(){ personDao.add(); } }

    (7)PersonServiceBean4.java

    public class PersonServiceBean4 implements PersonService { @Resource private PersonDao personDao; public PersonServiceBean4() {} public PersonServiceBean4(PersonDao personDao) { this.personDao = personDao; } public void save(){ personDao.add(); } }

    (8)PersonServiceBean5.java

    public class PersonServiceBean5 implements PersonService { /** * @Resource(name="personDao")指定注入name为personDao的bean,如果不存在则异常 */ @Resource(name="personDao") private PersonDao personDao; public PersonServiceBean5() {} public PersonServiceBean5(PersonDao personDao) { this.personDao = personDao; } public void save(){ personDao.add(); } }

    (9)PersonServiceBean6.java

    public class PersonServiceBean6 implements PersonService { private PersonDao personDao; @Resource public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public PersonServiceBean6() {} public PersonServiceBean6(PersonDao personDao) { this.personDao = personDao; } public void save(){ personDao.add(); } }

    (10)beans.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 使用<context:annotation-config/>隐式地向 Spring容器注册BeanPostProcessor --> <context:annotation-config/> <!-- 使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean --> <!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> --> <!-- 使用@Resource、@PostConstruct、@PreDestroy等注解,必须事先在Spring容器中声明CommonAnnotationBeanPostProcessor的Bean --> <!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> --> <bean id="personDao" class="com.spring.dao.impl.PersonDaoBean"></bean> <bean id="personService1" class="com.spring.service.impl.PersonServiceBean1"></bean> <bean id="personService2" class="com.spring.service.impl.PersonServiceBean2"></bean> <bean id="personService3" class="com.spring.service.impl.PersonServiceBean3"></bean> <bean id="personService4" class="com.spring.service.impl.PersonServiceBean4"></bean> <bean id="personService5" class="com.spring.service.impl.PersonServiceBean5"></bean> <bean id="personService6" class="com.spring.service.impl.PersonServiceBean6"></bean> </beans>

    (11)Test.java

    public class Test { @org.junit.Test public void test1() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService1"); personService.save(); } @org.junit.Test public void test2() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService2"); personService.save(); } @org.junit.Test public void test3() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService3"); personService.save(); } @org.junit.Test public void test4() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService4"); personService.save(); } @org.junit.Test public void test5() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService5"); personService.save(); } @org.junit.Test public void test6() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService6"); personService.save(); } }

    3、结果

    test1()运行结果:

    PersonDaoBean add

    test2()运行结果:

    PersonDaoBean add

    test3()运行结果:

    PersonDaoBean add

    test4()运行结果:

    PersonDaoBean add

    test5()运行结果:

    PersonDaoBean add

    test6()运行结果:

    PersonDaoBean add

    转载请注明原文地址: https://ju.6miu.com/read-970091.html

    最新回复(0)