一、使用注解配置Bean
1、注解
在类定义、方法定义、成员变量定义前使用。其简化<bean>标签,功能同<bean>标签。
格式为:
@注解标记名。
2、组件扫描
Spring可以从classpath(类路径)下自动扫描、实例化具有特殊注解的组件。
常用注解:
(1)@Repository 一般用于数据访问(数据访问(持久)层组件,实现dao访问)
(2)@Service 一般用于服务(业务层组件,处理业务逻辑,比如注入并操作dao)
(3)@Controller 一般用于控制器(控制层组件)
(4)@Component 一般用于自定义组件,把普通类实例化到spring容器中,相当于配置文件中的<bean />
(5)@Scope 用于控制对象创建,默认为单例模式。
(6)@PostConstruct 用于指定init-method方法。
(7)@PreDestroy 指定destroy-method方法。
(8)@Resource 用于注入bean对象
(9)@Autowired 基本等价于 @Resource
(10)@Qualifier("XXX") 与@Autowired连用,强制按名称匹配。
(11)@value("") Spring表达式注入。
需要在xml文件中配置自动扫描。
需要在applicationContext.xml中开启自动扫描。
其中base-package填写的是需要扫描的包名(可以只写一部分,比如base-package="com",那么将会扫描以com开头的所有包)。多个包名间用逗号(,)隔开。
<beans>
<context:component-scan base-package="com.test"></context:component-scan>
</beans>
ASP站长网对于扫描到的类,Spring默认命名为首字母小写。
即
@Controller
public class LoginController{
}
等价于
<bean class="XXX.LoginController"/>
3、@component
第一种形式:直接在类的前面写@component,其会自动将类名当做id,且首字母小写。
package com.test;
import org.springframework.stereotype.Component;
/**
* 扫描ExampleBean组件,默认id=exampleBean
* 相当于<bean ></bean>
* 注:id名的首字母小写。
*/
@Component
public class ExampleBean {
public void execute() {
System.out.println("执行execute处理方法1");
}
}
第二种形式:自定义id名。
package com.test;
import org.springframework.stereotype.Component;
/**
* 相当于<bean ></bean>
*
*/
@Component("test")
public class ExampleTest {
public void show() {
System.out.println("hello world");
}
}
4、@Scope
默认为单例模式(根据同一个id,getBean获取的都是同一个对象),可以采用@Scope("prototype") 来将其变成原型模式(根据同一个id,getBean获取的都是不同的对象)。
package com.test;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* 扫描ExampleBean组件,默认id=exampleBean
* 相当于<bean ></bean>
* 注:id名的首字母小写。
*/
@Component
@Scope("prototype") //等价于<bean scope="prototype">
public class ExampleBean {
public void execute() {
System.out.println("执行execute处理方法1");
}
}
5、@PostConstruct
设置初始化方法。
package com.test;
import Javax.annotation.PostConstruct;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype") //等价于<bean scope="prototype">
public class ExampleBean {
public void execute() {
System.out.println("执行execute处理方法1");
}
@PostConstruct //等价于<bean init-method="init">
public void init() {
System.out.println("初始化");
}
}
6、@PreDestroy
设置销毁方法。
package com.test;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
@Component
public class ExampleBean {
public void execute() {
System.out.println("执行execute处理方法1");
}
@PostConstruct //等价于<bean init-method="init">
public void init() {
System.out.println("初始化");
}
@PreDestroy //等价于<bean destory-method="destory">
public void destory() {
System.out.println("释放资源");
}
}
7、@Resource
其可以写在变量定义前,也可以写在setXXX方法前。@Resource默认按照ByName自动注入。@Resource有两个重要的属性:name和type,而Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以,如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不制定name也不制定type属性,这时将通过反射机制使用byName自动注入策略。
其导入包:import javax.annotation.Resource;
注:
使用注解时,若@Resource注解写在变量定义前,那么setXXX方法可以不写。
但若是在xml文件中采用<bean>标签的set注入,需要实现setXXX方法。
@Resource装配顺序:
step1:如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常。
step2:如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常。
step3:如果指定了type,则从上下文中找到类似匹配的唯一bean进行装配,找不到或是找到多个,都会抛出异常。
step4:如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配。
private Computer computer;
@Resource
private Phone phone; //插入类型为Phone的对象
@Resource //注入bean对象
public void setComputer(Computer computer) {
this.computer = computer;
}
8、@Autowired
基本等价于 @Resource。@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起使用
其导入包:import org.springframework.beans.factory.annotation.Autowired;
private Computer computer;
@Autowired
private Phone phone; //插入类型为Phone的对象
@Autowired //注入bean对象
public void setComputer(Computer computer) {
this.computer = computer;
}
9、@Resource与@Autowired的区别
@Resource的作用相当于@Autowired,只不过@Autowired默认按照byType自动注入。@Resource默认按 byName自动注入
注:
构造器注入时,只能使用@Autowired。
Set注入时,可以使用@Autowired或者@Resource(推荐)。
容器中存在多个同类型的对象时,若@Autowired放在setXX方法前,那么只依据类型(ByType)去匹配。若想使@Autowired 按照byName匹配,需与@Qualifier("")连用。
@Autowired
@Qualifier("computer")
private Computer computer;
@Resource放在setXXX方法前,会先依据XXX(默认ByName)去匹配,可以设置是否按照byName或者byType匹配。
@Resource(name="phone") //等价于 @Resource,根据byName匹配。