Spring注解:
@Resource:配置依赖,该标注有一个name属性,可以指定一个配置依赖的名字;
1.该注解可以用于属性或者方法上,但一般用于属性上。
2.该注解的作用范围是runtime。
3.该注解有一个属性name,默认值为“”。
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
<bean id="Person" class="com.answer.springFUXI.Person"></bean>
<bean id="Student" class="com.answer.springFUXI.Student"></bean>
</beans>
导入命名空间和规范:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
注解解析器:
<context:annotation-config></context:annotation-config>
例:
public class Student {
public void method1(){
System.out.println("student_annotation");
}
}
public class Person {
//@Autowired
//@Qualifier("Student")
@Resource(name = "Student")
private Student student;
public void method(){
this.student.method1();
}
}
@org.junit.Test
public void test(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("PAndS.xml");
Person person=(Person) applicationContext.getBean("Person");
person.method();
}
@Resource与@Autowired
@Resource与@Autowired都是做bean的注入时使用。其实@Resource并不是spring的注解,它是jdk扩展包javax.annotation.Resource中的,需要导入使用,但 spring支持该注解的使用。
1.@Autowired
@Autowired为Spring提供的注解,需要导入包org.springframework.beans.factory.annotation.Autowired;只按照byType注入。
@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。
如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起使用 ,如下:
@Autowired
@Qualifier("student_annotation") 等价于 @Resource(name="student_annotation")
2.@Resource
@Resource默认按照ByName自动注入,由J2EE提供,需要导入包javax.annotation.Resource。 @Resource有两个重要的属性:name和type,而Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。 所以,如果使用name属性,则使用byName的自动注入策略,而使用type属性时则 使用byType自动注入策略。 如果既不制定name也不制定type属性,这时将通过反射机制使用byName自动注入策略。
使用注解将一个类加入到Spring容器中:
xml配置文档:
@Component的使用规则
1.在spring的配置文件中导入命名空间及规范
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
2.引入类扫描及注解解析器
<context:component-scan base-package="com.olymtech.spring.annotation.scan"></context:component-scan>
a:该注解解析器包含了两个功能:依赖注入和类扫描(包含了<context:annotation-config></context:annotation-config>)
b:base-package:在该包及子包中扫描
3.如果一个类或者接口上(一般都是类上),加了@Component注解,就会进行如下法则:
如果其value属性的值为“”
@Component
public class Person(){
}
等价于
<bean id="person" class="......Person"></bean>(如果value值不写,默认的是类名首字母小写,其他不变)
如果其value属性的值不为“”
@Component("p")
public class Person(){
}
等价于
<bean id="p" class="......Person"></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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.answer.zhujie1"></context:component-scan>
</beans>
@Component("steelaxe")
public class SteelAxe {
public SteelAxe(){}
public void chop(){
System.out.println("quick");
}
}
@Component("chinese")
public class Chinese {
@Resource(name = "steelaxe")
private SteelAxe steelAxe;
public void use(){
this.steelAxe.chop();
}
}
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext1.xml");
SteelAxe steelAxe=(SteelAxe) applicationContext.getBean("steelaxe");
Chinese chinese=(Chinese) applicationContext.getBean("chinese",Chinese.class);
chinese.use();
}
常用注解:
--定义Bean的注解(类扫描注解)
@Controller
@Controller("Bean的名称")
定义控制层Bean,如Action
@Service
@Service("Bean的名称")
定义业务层Bean
@Repository
@Repository("Bean的名称")
定义DAO层Bean
@Component
定义Bean, 不好归类时使用.
--自动装配Bean (依赖注入注解;选用一种注解就可以)
@Autowired (Srping提供的)
默认按类型匹配,自动装配(Srping提供的),可以写在成员属性上,或写在setter方法上
@Autowired(required=true)
一定要找到匹配的Bean,否则抛异常。 默认值就是true
@Autowired
@Qualifier("bean的名字")
按名称装配Bean,与@Autowired组合使用,解决按类型匹配找到多个Bean问题。
@Resource JSR-250提供的
默认按名称装配,当找不到名称匹配的bean再按类型装配.
可以写在成员属性上,或写在setter方法上
可以通过@Resource(name="beanName") 指定被注入的bean的名称, 要是未指定name属性, 默认使用成员属性的变量名,一般不用写name属性.
@Resource(name="beanName")指定了name属性,按名称注入但没找到bean, 就不会再按类型装配了.
@Inject 是JSR-330提供的
按类型装配,功能比@Autowired少,没有使用的必要。
--定义Bean的作用域和生命过程
@Scope("prototype")
值有:singleton,prototype,session,request,session,globalSession
@PostConstruct
相当于init-method,使用在方法上,当Bean初始化时执行。
@PreDestroy
相当于destory-method,使用在方法上,当Bean销毁时执行。
--声明式事务
@Transactional