一:注释的方法注入
1.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/>
</beans>
二:@Autowired或 @Resource注释方式进行装配
@Autowired是默认按类型装配。
@Resource是默认按名称装配,找不到与名称匹配的bean时,再按类型装配。
(1).@Resource: JDK提供,移植性强,推荐使用, 可以看到支撑类是: import javax.annotation.Resource; 只要是JDK1.5以上版本,JDK内置了此注释支撑类.
(2).用法:
用在属性上
示例:
//dao层接口:
package com.spring.chapter5.dao;
public interface FruitDao {
public void create();
}
dao层实现类
package com.spring.chapter5.dao.impl;
import com.spring.chapter5.dao.FruitDao;
public class FruitDaoImpl implements FruitDao {
public void create() {
System.out.println("Dao层方法create被调用");
}
}
service层接口
package com.spring.chapter5.service;
public interface FruitService {
public void create();
}
service层实现类
package com.spring.chapter5.service.impl;
import javax.annotation.Resource;
import com.spring.chapter5.dao.FruitDao;
import com.spring.chapter5.service.FruitService;
public class FruitServiceImpl implements FruitService {
@Resource
private FruitDao fruitDao;
public void create() {
fruitDao.create();
}
public void setFruitDao(FruitDao fruitDao) {
this.fruitDao = fruitDao;
}
}
配置文件(chapter5.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 />
<bean id="fruitDao" class="com.spring.chapter5.dao.impl.FruitDaoImpl" />
<bean id="fruitService" class="com.spring.chapter5.service.impl.FruitServiceImpl" />
</beans>
注意:我们只在xml中配置了两个bean对象,它们之间并没有关系,但是在Java中我们采用注释语法,这个时候@Resource注释语法会根据属性名称在spring容器中去寻找和属性名称同名id的bean对象,然后将其注入到属性中。
//测试类
package com.spring.chapter5.service.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.chapter5.service.FruitService;
public class Test {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("chapter5.xml");
FruitService fruitService = (FruitService) act.getBean("fruitService");
fruitService.create();
}
}
(3).更改配置文件
将<bean id="fruitDao" class="com.spring.chapter5.dao.impl.FruitDaoImpl" />改为<bean id="myFruitDao" class="com.spring.chapter5.dao.impl.FruitDaoImpl" />
这个时候可以看到myFruitDao和属性名称并没有对应,但执行Test文件后,可以看到执行成功。这因为如果名称上没有对应,就会按类型自动对性,所以会执行成功。
(4).我们还可以给@Resource加上name,请看下面的语法
//service层实现类
public class FruitServiceImpl implements FruitService {
@Resource(name="myFruitDao")
private FruitDao fruitDao;
public void create() {
fruitDao.create();
}
public void setFruitDao(FruitDao fruitDao) {
this.fruitDao = fruitDao;
}
}
再次执行Test,同样可以执行成功.
(5).用上属性的set方法上
//service层实现类
public class FruitServiceImpl implements FruitService {
private FruitDao fruitDao;
public void create() {
fruitDao.create();
}
@Resource(name="myFruitDao")
public void setFruitDao(FruitDao fruitDao) {
this.fruitDao = fruitDao;
}
}
可以看到,用在属性上,或者用上属性的set方法上,效果是一模一样的,没有任何区别.
三:@Autowired
//service层实现类
public class FruitServiceImpl implements FruitService {
private FruitDao fruitDao;
public void create() {
fruitDao.create();
}
@Autowired
public void setFruitDao(FruitDao fruitDao) {
this.fruitDao = fruitDao;
}
}
运行Test文件,可以看到效果一样
同样,我们也可以把@Autowired改成按名称装配
//service层实现类
public class FruitServiceImpl implements FruitService {
@Autowired()
@Qualifier("myFruitDao")
private FruitDao fruitDao;
public void create() {
fruitDao.create();
}
public void setFruitDao(FruitDao fruitDao) {
this.fruitDao = fruitDao;
}
}
这里需要注意的是:
@Autowired()
@Qualifier("myaFruitDao")
private FruitDao fruitDao;
这两句只能用在属性上,不能用上方法上.
@Autowired的required属性
@Autowired(required=true)
@Qualifier("myFruitDao")
required=true : 必须为属性注入值,
required=false: 不是必须注入值,如果找不到对应的值注入,会注入null
四:通过在classpath自动扫描方式把组件纳入到spring容器中管理.
Spring2.5引入了组件自动扫描机制,他可以在类路径下寻找标注了@Component, @Service, @Controller, @Repository注解的类, 并把这些类纳入进Spring容器中管理,它的作用,和在XML文件中使用Bean节点配置组件是一样的,要使用自动扫描机制。
<?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:component-scan base-package="com.spring.chapter5" />
</beans>
<context:component-scan base-package="com.spring.chapter5" />这一项配置了需要扫描的包,包含子包.
@Service: 用于标注业务层的类
@Controller: 用于标注控制层,如Action
@Repository: 用于标注Dao层
@Component: 用于标注组件层, 当组件不好归类的时候,可以用此标注
通过扫描机制, 基本上可以达到配置文件基本为空的效果, 这是Spring2.5新推出的功能, 老系统不能运用, 但是新系统中, 此功能运用的非常多。
示例:
dao层实现类
package com.spring.chapter5.dao.impl;
import org.springframework.stereotype.Repository;
import com.spring.chapter5.dao.FruitDao;
@Repository
public class FruitDaoImpl implements FruitDao{
public void create() {
System.out.println("Dao层方法create被调用");
}
}
service层实现类
package com.spring.chapter5.service.impl;
import org.springframework.stereotype.Service;
import com.spring.chapter5.dao.FruitDao;
import com.spring.chapter5.service.FruitService;
@Service
public class FruitServiceImpl implements FruitService {
private FruitDao fruitDao;
public void create() {
fruitDao.create();
}
public void setFruitDao(FruitDao fruitDao) {
this.fruitDao = fruitDao;
}
}
这个时候,我将Dao层,服务层的两个实现类交给Spring管理了,相当于以前配置了两个Bean,这个时候我们可以验证一下,我们是否可以从容器中获取对象。
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:component-scan base-package="com.spring.chapter5" />
</beans>
测试类
package com.spring.chapter5.service.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.chapter5.dao.FruitDao;
import com.spring.chapter5.service.FruitService;
public class Test {
public static void main(String[] args) {
ApplicationContext acx = new ClassPathXmlApplicationContext(
"chapter5.xml");
FruitDao fruitDao = (FruitDao) acx.getBean("fruitDaoImpl");
FruitService fruitService = (FruitService) acx
.getBean("fruitServiceImpl");
System.out.println(fruitDao);
System.out.println(fruitService);
}
}
这里需要注意的是,当对象交给Spring管理后,Spring给对象起的ID名称是类的简单名称, 即类全名,但是首字母小写。
自定义名称:
dao层实现类
package com.spring.chapter5.dao.impl;
import org.springframework.stereotype.Repository;
import com.spring.chapter5.dao.FruitDao;
@Repository("fruitDao")
public class FruitDaoImpl implements FruitDao {
public void create() {
System.out.println("Dao层方法create被调用");
}
}
service层实现类
package com.spring.chapter5.service.impl;
import org.springframework.stereotype.Service;
import com.spring.chapter5.dao.FruitDao;
import com.spring.chapter5.service.FruitService;
@Service("fruitService")
public class FruitServiceImpl implements FruitService {
private FruitDao fruitDao;
public void create() {
fruitDao.create();
}
public void setFruitDao(FruitDao fruitDao) {
this.fruitDao = fruitDao;
}
}
测试类
package com.spring.chapter5.service.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.chapter5.dao.FruitDao;
import com.spring.chapter5.service.FruitService;
public class Test {
public static void main(String[] args) {
ApplicationContext acx = new ClassPathXmlApplicationContext(
"chapter2.xml");
FruitDao fruitDao = (FruitDao)acx.getBean("fruitDao");
FruitService fruitService = (FruitService) acx.getBean("fruitService");
System.out.println(fruitDao);
System.out.println(fruitService);
}
}
五:改变对象的范围(单例或非单例)
@Repository("fruitDao")
@Scope("prototype")
加上@Scope("prototype")此注释即可,这个时候每次获取对象都会是一个新对象
六:对象初始化方法和销毁方法的配置
@Repository("fruitDao")
@Scope("prototype")
public class FruitDaoImpl implements FruitDao {
public void create() {
System.out.println("Dao层方法create被调用");
}
@PostConstruct
public void init(){
System.out.println("调用init");
}
@PreDestroy
public void destory(){
System.out.println("调用destory");
}
}
如果对象是非单例模式,这个时候可以看到init方法会调用两次,而destroy方法不会调用.
将Dao层注入到服务层:
@Service("fruitService")
public class FruitServiceImpl implements FruitService {
@Resource(name="fruitDao")
private FruitDao fruitDao;
public void create() {
fruitDao.create();
}
public void setFruitDao(FruitDao fruitDao) {
this.fruitDao = fruitDao;
}
}