初学SpringMVC+Mybatis之Spring基于注解的组件扫描
2016-05-06 00:12
543 查看
转自:http://blog.csdn.net/honchen2010/article/details/50984566
1、什么是组件扫描
指定一个包路径,Spring会自动扫描该包及其子包所有组件类,当发现组件类定义前有特定的注解标记时,就将该组件纳入到Spring容器中,等价于原来的XML配置bean的功能。
2、指定扫描类路径
使用组件扫描,首先需要在XML配置中指定扫描父级package路径,容器会自动去扫描pers.zky包及其子包下的所有组件,并且实例化bean
[html] view
plain copy
<!-- 1、开始扫描,指定扫描的包,此处指定的是pers.zky下面所有子包 -->
<context:component-scan base-package="pers.zky"></context:component-scan>
3、自动扫描的注解标记
指定扫描的类路径后,并且不是所有的该路径下的组件都会被扫描到Spring容器,只有在组件类定义前有以下标记的,才会扫描到Spring容器。
@Component 通用注解
@Named 通用注解
@Respository 持久化层组件注解
@Service 业务层组件注解
@Controller 控制层组件注解
4、自动扫描注解命名
当一个组件在扫描过程中被检测到时,会生成一个默认的id值,默认id值为小写开头的类名,也可以在注解中自定义id。
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*@Component//这种注解,扫描时会自动指定其id为首字母小写后的类名,此处即为"book"*/
@Component("b")//指定注解扫描后的id为"b"
public class Book implements Serializable{
private int id;
private String name;
public Book(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[java] view
plain copy
@Test
public void test(){
String conf="applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
/**
* Book实体类使用的是注解注入没有指定类的id时
* Spring规定了注解注入经过组件扫描后,其id为类名首字母小写后的名字
*/
/*Book book = ac.getBean("book",Book.class);*/
Book book = ac.getBean("b",Book.class);
System.out.println(book);//pers.zky.entity.Book@1738a82
System.out.println(book.getId());//0
System.out.println(book.getName());//null
5、指定组件的作用域
通常Spring管理的组件,默认的作用域是"singleton",如果需要其他的作用域也可以使用@Scope注解,只要在注解中提供作用域的名称即可。
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* @author Zky
*
*/
@Scope("prototype")
@Component("student")
public class Student implements Serializable{
private String name;
private int id;
private Book book;
public Student(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Book getBook() {
return book;
}
}
[java] view
plain copy
Student stu = ac.getBean("student",Student.class);
Student stu1 = ac.getBean("student",Student.class);
System.out.println(stu==stu1);//false,未指定Scope的属性时默认为sington,输出为true
6、指定初始化和销毁方法
@PostConstruct 指定初始化方法
@PreDestroy 指定销毁方法
[java] view
plain copy
@PostConstruct//指定初始化方法
public void init(){
System.out.println("student初始化");
}
@PreDestroy//指定销毁方法
public void destroy(){
System.out.println("student销毁");
}
7、指定依赖注入关系
具有依赖关系的bean对象,利用下面任意一种注解都可以实现关系的注入。
1)、基本类型的值注入可以使用 @value() 标记
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("b")
public class Book implements Serializable{
@Value("10001")
private int id;
@Value("西游记")
private String name;
public Book(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2)、@Autowired/@Qualifier可以处理构造器注入和Setter注入
@Autowired写在构造器前面,申明需要为其注入bean。
@Qualifier写在参数前面,申明需要注入的bean的id,注入对象单例时,@Qualifier可以省略。
@Autowired写在属性上面,只会执行构造器的赋值语句,其他代码不会执行。
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author Zky
*/
@Component("student")
public class Student implements Serializable{
@Value("张三")
private String name;
@Value("1001")
private int id;
@Autowired
private Book book;
public Student(){
}
public Student(Book book){
System.out.println("before");
this.book = book;
System.out.println("after");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
System.out.println("before_getbook");
this.book = book;
System.out.println("after_getbook");
}
}
[java] view
plain copy
@Test
public void test(){
String conf="applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
Student stu = ac.getBean("student",Student.class);
System.out.println(stu.getBook().getName());
}
Console:
西游记
@Autowired写在构造方法或者set方法上时,会执行里面全部的代码。下面是Student类的@Autowired改写到构造器或者set方法上后的输出结果,Test方法和上面一样。
[java] view
plain copy
@Autowired
public Student(Book book){
System.out.println("before");
this.book = book;
System.out.println("after");
}
Console:
before
after
西游记
[java] view
plain copy
<pre name="code" class="java">@Autowired
public void setBook(Book book){
System.out.println("before_setbook");
this.book = book;
System.out.println("after_setbook");
}
Console:
before_setbook
after_setbook
西游记
3)、使用@Resouce注解
只能用在属性和set方法上上。
用在set 方法中 ,执行所有的方法体。
用在属性上只执行方法的赋值。
@Resouce写在构造器上会报语法错误:The annotation @Resource is disallowed for this location
同样使用上面的例子,删掉@Autowired注解,使用@Resouce注解set方法和属性:
[java] view
plain copy
@Resource
private Book book;
Console:
西游记
[java] view
plain copy
<pre name="code" class="java">@Resource
public void setBook(Book book) {
System.out.println("before_setbook");
this.book = book;
System.out.println("after_setbook");
}
Console:
before_setbook
after_setbook
西游记
Setter注入推荐使用@Resource,构造器推荐使用@Autowired。
4)、注入spring的表达式
将db.properties至于src文件目录下,其内容如下:
[plain] view
plain copy
username=scott
password=scott
jdbc=jdbc:oracle:thin:localhost:1521:orcl
driver=oracle.jdbc.OracleDriver
声明properties集合,读取参数:
[html] view
plain copy
<util:properties id="jdbc" location="classpath:db.properties"></util:properties>
编写实体类,用spring表达式注入值:
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class JdbcOracle implements Serializable{
@Value("#{jdbc.username}")
private String username;
@Value("#{jdbc.password}")
private String password;
@Value("#{jdbc.url}")
private String url;
@Value("#{jdbc.driver}")
private String driver;
public JdbcOracle(){
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
}
编写测试方法,输出结果:
[java] view
plain copy
@Test
public void test(){
String conf="applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
JdbcOracle jdbcOracle = ac.getBean("jdbcOracle",JdbcOracle.class);
System.out.println(jdbcOracle.getUsername());
System.out.println(jdbcOracle.getPassword());
System.out.println(jdbcOracle.getUrl());
System.out.println(jdbcOracle.getDriver());
}
Console:
scott
scott
jdbc:oracle:thin:localhost:1521:orcl
oracle.jdbc.OracleDriver
1、什么是组件扫描
指定一个包路径,Spring会自动扫描该包及其子包所有组件类,当发现组件类定义前有特定的注解标记时,就将该组件纳入到Spring容器中,等价于原来的XML配置bean的功能。
2、指定扫描类路径
使用组件扫描,首先需要在XML配置中指定扫描父级package路径,容器会自动去扫描pers.zky包及其子包下的所有组件,并且实例化bean
[html] view
plain copy
<!-- 1、开始扫描,指定扫描的包,此处指定的是pers.zky下面所有子包 -->
<context:component-scan base-package="pers.zky"></context:component-scan>
3、自动扫描的注解标记
指定扫描的类路径后,并且不是所有的该路径下的组件都会被扫描到Spring容器,只有在组件类定义前有以下标记的,才会扫描到Spring容器。
@Component 通用注解
@Named 通用注解
@Respository 持久化层组件注解
@Service 业务层组件注解
@Controller 控制层组件注解
4、自动扫描注解命名
当一个组件在扫描过程中被检测到时,会生成一个默认的id值,默认id值为小写开头的类名,也可以在注解中自定义id。
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*@Component//这种注解,扫描时会自动指定其id为首字母小写后的类名,此处即为"book"*/
@Component("b")//指定注解扫描后的id为"b"
public class Book implements Serializable{
private int id;
private String name;
public Book(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[java] view
plain copy
@Test
public void test(){
String conf="applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
/**
* Book实体类使用的是注解注入没有指定类的id时
* Spring规定了注解注入经过组件扫描后,其id为类名首字母小写后的名字
*/
/*Book book = ac.getBean("book",Book.class);*/
Book book = ac.getBean("b",Book.class);
System.out.println(book);//pers.zky.entity.Book@1738a82
System.out.println(book.getId());//0
System.out.println(book.getName());//null
5、指定组件的作用域
通常Spring管理的组件,默认的作用域是"singleton",如果需要其他的作用域也可以使用@Scope注解,只要在注解中提供作用域的名称即可。
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* @author Zky
*
*/
@Scope("prototype")
@Component("student")
public class Student implements Serializable{
private String name;
private int id;
private Book book;
public Student(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Book getBook() {
return book;
}
}
[java] view
plain copy
Student stu = ac.getBean("student",Student.class);
Student stu1 = ac.getBean("student",Student.class);
System.out.println(stu==stu1);//false,未指定Scope的属性时默认为sington,输出为true
6、指定初始化和销毁方法
@PostConstruct 指定初始化方法
@PreDestroy 指定销毁方法
[java] view
plain copy
@PostConstruct//指定初始化方法
public void init(){
System.out.println("student初始化");
}
@PreDestroy//指定销毁方法
public void destroy(){
System.out.println("student销毁");
}
7、指定依赖注入关系
具有依赖关系的bean对象,利用下面任意一种注解都可以实现关系的注入。
1)、基本类型的值注入可以使用 @value() 标记
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("b")
public class Book implements Serializable{
@Value("10001")
private int id;
@Value("西游记")
private String name;
public Book(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2)、@Autowired/@Qualifier可以处理构造器注入和Setter注入
@Autowired写在构造器前面,申明需要为其注入bean。
@Qualifier写在参数前面,申明需要注入的bean的id,注入对象单例时,@Qualifier可以省略。
@Autowired写在属性上面,只会执行构造器的赋值语句,其他代码不会执行。
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author Zky
*/
@Component("student")
public class Student implements Serializable{
@Value("张三")
private String name;
@Value("1001")
private int id;
@Autowired
private Book book;
public Student(){
}
public Student(Book book){
System.out.println("before");
this.book = book;
System.out.println("after");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
System.out.println("before_getbook");
this.book = book;
System.out.println("after_getbook");
}
}
[java] view
plain copy
@Test
public void test(){
String conf="applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
Student stu = ac.getBean("student",Student.class);
System.out.println(stu.getBook().getName());
}
Console:
西游记
@Autowired写在构造方法或者set方法上时,会执行里面全部的代码。下面是Student类的@Autowired改写到构造器或者set方法上后的输出结果,Test方法和上面一样。
[java] view
plain copy
@Autowired
public Student(Book book){
System.out.println("before");
this.book = book;
System.out.println("after");
}
Console:
before
after
西游记
[java] view
plain copy
<pre name="code" class="java">@Autowired
public void setBook(Book book){
System.out.println("before_setbook");
this.book = book;
System.out.println("after_setbook");
}
Console:
before_setbook
after_setbook
西游记
3)、使用@Resouce注解
只能用在属性和set方法上上。
用在set 方法中 ,执行所有的方法体。
用在属性上只执行方法的赋值。
@Resouce写在构造器上会报语法错误:The annotation @Resource is disallowed for this location
同样使用上面的例子,删掉@Autowired注解,使用@Resouce注解set方法和属性:
[java] view
plain copy
@Resource
private Book book;
Console:
西游记
[java] view
plain copy
<pre name="code" class="java">@Resource
public void setBook(Book book) {
System.out.println("before_setbook");
this.book = book;
System.out.println("after_setbook");
}
Console:
before_setbook
after_setbook
西游记
Setter注入推荐使用@Resource,构造器推荐使用@Autowired。
4)、注入spring的表达式
将db.properties至于src文件目录下,其内容如下:
[plain] view
plain copy
username=scott
password=scott
jdbc=jdbc:oracle:thin:localhost:1521:orcl
driver=oracle.jdbc.OracleDriver
声明properties集合,读取参数:
[html] view
plain copy
<util:properties id="jdbc" location="classpath:db.properties"></util:properties>
编写实体类,用spring表达式注入值:
[java] view
plain copy
package pers.zky.entity;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class JdbcOracle implements Serializable{
@Value("#{jdbc.username}")
private String username;
@Value("#{jdbc.password}")
private String password;
@Value("#{jdbc.url}")
private String url;
@Value("#{jdbc.driver}")
private String driver;
public JdbcOracle(){
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
}
编写测试方法,输出结果:
[java] view
plain copy
@Test
public void test(){
String conf="applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
JdbcOracle jdbcOracle = ac.getBean("jdbcOracle",JdbcOracle.class);
System.out.println(jdbcOracle.getUsername());
System.out.println(jdbcOracle.getPassword());
System.out.println(jdbcOracle.getUrl());
System.out.println(jdbcOracle.getDriver());
}
Console:
scott
scott
jdbc:oracle:thin:localhost:1521:orcl
oracle.jdbc.OracleDriver
相关文章推荐
- Java根据sessionId获取Session对象
- spring-core组件详解——Environment应用上下文环境
- Java开发环境的搭建及第一个Java程序的创建
- 关于Java多态的理解。
- how to use java annotation
- WebSphere:Javadump和javacore文件分析工具
- 如何分析websphere中间件生成的javacore文件
- springmvc+Mybatis整合中sqlSession的创建问题
- java四类八种基本类型
- JAVA 相关
- 五种Spring事务配置的方式
- Spring mvc 日期转换 支持多种格式
- 69道Spring面试题和解答
- Java实现FTP相关操作
- Reverse String JAVA
- Maven profile结合Maven AntRun Plugin实现不同环境配置文件分离打包
- Java IO流之简单使用方法
- java获取路径
- Java IO流之字符流框架图
- JavaIO流之字节流框架图