Spring IOC的注解使用
在之前的项目中-我们都是通过xml文件进行bean或者某些属性的赋值-其实还有另外一种注解的方式-在企业开发中使用的很多-在bean上添加注解-可以快速的将bean注册到ioc容器。
1、使用注解的方式注册bean到IOC容器中
applicationContext.xml
PersonController.java
``java
package com.oi.controller;import org.springframework.stereotype.Controller;@Controllerpublic class PersonController { public PersonController() { System.out.println("创建对象"); }}
`
PersonService.java
`java
package com.oi.service;import org.springframework.stereotype.Service;@Servicepublic class PersonService {}
`
PersonDao.java
`java
package com.oi.dao;import org.springframework.stereotype.Repository;@Repository("personDao")@Scope(value="prototype")public class PersonDao {}
`
2、定义扫描包时要包含的类和不要包含的类
当定义好基础的扫描包后-在某些情况下可能要有选择性的配置是否要注册bean到IOC容器中-此时可以通过如下的方式进行配置。
applicationContext.xml
3、使用@AutoWired进行自动注入
使用注解的方式实现自动注入需要使用@AutoWired注解。
PersonController.java
`java
package com.oi.controller;import com.oi.service.PersonService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;@Controllerpublic class PersonController { @Autowired private PersonService personService; public PersonController() { System.out.println("创建对象"); } public void getPerson(){ personService.getPerson(); }}
`
PersonService.java
`java
package com.oi.service;import com.oi.dao.PersonDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class PersonService { @Autowired private PersonDao personDao; public void getPerson(){ personDao.getPerson(); }}
`
PersonDao.java
`java
package com.oi.dao; import org.springframework.stereotype.Repository;@Repositorypublic class PersonDao { public void getPerson(){ System.out.println("PersonDao:getPerson"); }}
`
注意:当使用AutoWired注解的时候-自动装配的时候是根据类型实现的。
1、如果只找到一个-则直接进行赋值-
2、如果没有找到-则直接抛出异常-
3、如果找到多个-那么会按照变量名作为id继续匹配,
1、匹配上直接进行装配
2、如果匹配不上则直接报异常
PersonServiceExt.java
`java
package com.oi.service;import com.oi.dao.PersonDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class PersonServiceExt extends PersonService{ @Autowired private PersonDao personDao; public void getPerson(){ System.out.println("PersonServiceExt......"); personDao.getPerson(); }}
`
PersonController.java
`java
package com.oi.controller;import com.oi.service.PersonService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;@Controllerpublic class PersonController { @Autowired private PersonService personServiceExt; public PersonController() { System.out.println("创建对象"); } public void getPerson(){ personServiceExt.getPerson(); }}
`
还可以使用@Qualifier注解来指定id的名称-让spring不要使用变量名,当使用@Qualifier注解的时候也会有两种情况:
1、找到-则直接装配
2、找不到-就会报错
PersonController.java
`java
package com.oi.controller;import com.oi.service.PersonService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;@Controllerpublic class PersonController { @Autowired @Qualifier("personService") private PersonService personServiceExt2; public PersonController() { System.out.println("创建对象"); } public void getPerson(){ personServiceExt2.getPerson(); }}
`
通过上述的代码我们能够发现-使用@AutoWired肯定是能够装配上的-如果装配不上就会报错。
4、@AutoWired可以进行定义在方法上
当我们查看@AutoWired注解的源码的时候发现-此注解不仅可以使用在成员变量上-也可以使用在方法上。
PersonController.java
`java
package com.oi.controller;import com.oi.dao.PersonDao;import com.oi.service.PersonService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;@Controllerpublic class PersonController { @Qualifier("personService") @Autowired private PersonService personServiceExt2; public PersonController() { System.out.println("创建对象"); } public void getPerson(){ System.out.println("personController..."+personServiceExt2);// personServiceExt2.getPerson(); } / * 当方法上有@AutoWired注解时: * 1、此方法在bean创建的时候会自动调用 * 2、这个方法的每一个参数都会自动注入值 * @param personDao */ @Autowired public void test(PersonDao personDao){ System.out.println("此方法被调用:"+personDao); } /