观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-8

 

注解在set方法上:

package com.imooc.beanannotation.jsr;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service
public class JsrService {

//@Resource
	private JsrDAO jsrDAO;
	
@Resource
	public void setJsrDAO(JsrDAO jsrDAO) {
	this.jsrDAO = jsrDAO;
}

	public void save(){
		jsrDAO.save();
	}
}
package com.imooc.beanannotation.jsr;

import org.springframework.stereotype.Repository;

@Repository
public class JsrDAO {
	
	public void save(){
		System.out.println("JsrDAO invoked.");
	}

}
package com.imooc.test.beanannotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.beanannotation.jsr.JsrService;
import com.imooc.ioc.interfaces.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestJsr extends UnitTestBase {
	
	public TestJsr(){
		super("classpath*:spring-beanannotation.xml");
}
	
	@Test
	public void testSave(){
		JsrService service = getBean("jsrService");
		service.save();
	}
}

运行结果:

注解在成员变量上:

注意:无论这两种方式中的哪一种,都能讲DAO的实例,注入到当前的实例里来。

 

@PostConstruct&@PreDestroy

[email protected]说明

     被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。

[email protected]说明

     被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

package com.imooc.beanannotation.jsr;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service
public class JsrService {

@Resource
	private JsrDAO jsrDAO;
	
//@Resource
	public void setJsrDAO(JsrDAO jsrDAO) {
	this.jsrDAO = jsrDAO;
}

	@PostConstruct
	public void init(){
		System.out.println("JsrService init.");
	}
	
	@PreDestroy
	public void destroy(){
		System.out.println("JsrService destroy.");
	}

	public void save(){
		jsrDAO.save();
	}
}

运行结果:

@Named&@Inject:

package com.imooc.beanannotation.jsr;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.stereotype.Service;

//@Service
@Named
public class JsrService {

//@Resource
	@Inject
	private JsrDAO jsrDAO;
	
//@Resource
	public void setJsrDAO(JsrDAO jsrDAO) {
	this.jsrDAO = jsrDAO;
}

	@PostConstruct
	public void init(){
		System.out.println("JsrService init.");
	}
	
	@PreDestroy
	public void destroy(){
		System.out.println("JsrService destroy.");
	}

	public void save(){
		jsrDAO.save();
	}
}

运行结果:

 

默认方式:

package com.imooc.beanannotation.jsr;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
//javax.inject包下载:http://mvnrepository.com/artifact/javax.inject/javax.inject/1
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.stereotype.Service;

//@Service
@Named
public class JsrService {

//@Resource
	//@Inject
	private JsrDAO jsrDAO;
	
//@Resource
	@Inject
	public void setJsrDAO(JsrDAO jsrDAO) {
	this.jsrDAO = jsrDAO;
}

	@PostConstruct
	public void init(){
		System.out.println("JsrService init.");
	}
	
	@PreDestroy
	public void destroy(){
		System.out.println("JsrService destroy.");
	}

	public void save(){
		jsrDAO.save();
	}
}

运行结果:

Named注解使用:

package com.imooc.beanannotation.jsr;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
//javax.inject包下载:http://mvnrepository.com/artifact/javax.inject/javax.inject/1
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.stereotype.Service;

//@Service
@Named
public class JsrService {

//@Resource
	//@Inject
	private JsrDAO jsrDAO;
	
//@Resource
	@Inject
	public void setJsrDAO(@Named("jsrDAO")JsrDAO jsrDAO) {
	this.jsrDAO = jsrDAO;
}

	@PostConstruct
	public void init(){
		System.out.println("JsrService init.");
	}
	
	@PreDestroy
	public void destroy(){
		System.out.println("JsrService destroy.");
	}

	public void save(){
		jsrDAO.save();
	}
}

运行结果:

注意:当一个接口有两个类时,可以用@Named来注解。

 

回顾:

原文链接:加载失败,请重新获取