基于注解的依赖注入(零配置)

基于注解的依赖注入(零配置)
基于注解(Annotation)的配置有越来越流行的趋势,注解配置先对于XML配置具有很多的优势:
它可以充分利用java的反射机制获取类结构信息,这些信息可以有效减少配置的工作。
注释和java文件处于同一个文件中,而XML配置采用独立的配置文件,大多数配置文 件信息在程序开发完成后不会调整,如果配置信息和java代码放在一起,有助于增强 程序的内聚性。而采用独立的XML配置文件,程序员在编写一个功能时,往往需要在 程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。
开发步骤:
导入common-annotations.jar架包
在ApplicationContext.xml中加入命名空间
Eg:
<?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>



Resource注解
以前我们通过xml文件方式表示Bean之间的依赖关系,而现在我们可以通过@Resource方式来标识Bean之间的依赖关系。
Person.java、
package net.battier.pojo;

import java.io.Serializable;

import javax.annotation.Resource;

public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String personName;
private boolean personSex;
private int personAge;
private String personDesc;
// 通过@Resource方式将配置文件中id为org的Bean注入给属性org;
//这种方式替代了xml文件中以ref关联Bean的方式。
@Resource(name="org")
private Organization org;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

。。。

}


在配置文件ApplicationContext.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 />
<!-- Organization -->
<bean id="org" class="net.battier.pojo.Organization"></bean>
<!-- Person (这里不再注入org,而是在类中通过注解方式进行注入)-->
<bean id="person" class="net.battier.pojo.Person"></bean>
</beans>


@PostConstruct和@PreDestroy注解
这两个是定制Bean的生命周期行为的,除了这两个,还可以通过实现InitializingBean或者DispasableBean接口来定制初始化或者销毁的行为,另外还可以在配置文件中,每个Bean的节点里加入init-method或者destroy-method来操作
Chinese.java
package net.battier.dao.impl;

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

import net.battier.dao.Axe;
import net.battier.dao.Person;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Chinese implements Person, InitializingBean, DisposableBean {

private Axe axe;

public Chinese() {
System.out.println("Spring实例化主调Bean:Chinese实例...");
}

// 通过设值注入
public void setAxe(Axe axe) {
this.axe = axe;
}

@Override
public void userAxe() {
// TODO Auto-generated method stub
axe.chop();
}

@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("正在初始化。。。");
}

// 定义销毁之前的特定行为
public void destroy() {
System.out.println("我快要挂了...");
}

@PostConstruct
public void init() {
System.out.println("使用注解初始化Chinese");
}

@PreDestroy
public void destroy1() {
System.out.println("使用注解销毁Chinese");
}
}



ApplicationContext.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 />
<!-- 配置chinese的实例并加上初始化方法 init-method="init"-->
<bean id="chinese" class="net.battier.dao.impl.Chinese"
destroy-method="destroy">
<!-- 通过设置注入 -->
<property name="axe" ref="sleelAxe"></property>
</bean>
<!-- 配置stoneAxe实例 -->
<bean id="sleelAxe" class="net.battier.dao.impl.SleelAxe"></bean>
</beans>


@Component注解
虽然我们可以通过@Resource在Bean类中使用自动注入功能,但是Bean还是在XML文件中通过<bean.../>进行定义,也就是说,在XML配置文件中定义Bean,通过@Resource为Bean的成员变量,方法入参提供自动注入的功能,能否也通过注解定义Bean,从XML配置文件中完全移除Bean定义的配置呢?答案是肯定的。

Organization.java
package net.battier.pojo;

import java.io.Serializable;

import org.springframework.stereotype.Component;

@Component("org")
public class Organization implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String orgName;
private String parent;
private String orgCode;
private String orgDesc;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

...

}


Person .java
package net.battier.pojo;

import java.io.Serializable;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component("person")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String personName;
private boolean personSex;
private int personAge;
private String personDesc;

// 通过@Resource方式将配置文件中id为org的Bean注入给属性org;
// 这种方式替代了xml文件中以ref关联Bean的方式。
@Resource(name = "org")
private Organization org;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

...


}


配置文件(注意要定义类扫描器,不需要命名空间)
<?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="net.battier.pojo"></context:component-scan>

<!-- Organization 使用了@Component注释,不需要在XML中配置了-->
<!-- <bean id="org" class="net.battier.pojo.Organization"></bean> -->

<!-- Person (这里不再注入org,而是在类中通过注解方式进行注入)-->
<!-- 使用了@Component注释,不需要在XML中配置了 -->
<!-- <bean id="person" class="net.battier.pojo.Person"></bean> -->
</beans>


在定义类扫描器
<context:component-scan base-package="net.battier.pojo">
</context:component-scan>
时,指定了要扫描的包下的类名,<context:component-scan/>还允许定义过滤器,从而将基包下的某些类纳入或排除。Spring支持四种类型的过滤方式:
注释:假如net.battier.Person是一个注解类,我们可以将使用该注解的类过滤出来。
类名指定:通过全限定类名进行过滤,如可以将某个类纳入扫描,也可以将某个类排除在外。
正则表达式:通过正则表达式定义过滤的类
AspectJ表达式:通过AspectJ标识定义过滤的类
Eg:

<context:component-scan base-package="net.battier.pojo">
<context:include-filter type="regex" expression="net\.battier\.service\.."/>
<context:exclude-filter type="aspectj" expression="net.battier.util.."/>
</context:component-scan>



@Scope注释
默认情况下,@Component定义的Bean都是Singleton的,如果需要使用其他作用范围的Bean,可以通过@Scope来定义。
package net.battier.pojo;

import java.io.Serializable;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("person")
@Scope("Singleton")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String personName;
private boolean personSex;
private int personAge;
private String personDesc;

// 通过@Resource方式将配置文件中id为org的Bean注入给属性org;
// 这种方式替代了xml文件中以ref关联Bean的方式。
@Resource(name = "org")
private Organization org;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

...

}




其他注解
Spring2.5中除了提供@Component注解,还定义了几个拥有特殊语义的注解:@Reponsetory、@Service和@Controller.在目前的Spring版本中,这三个注解和@Component作用是等效的。但是从注解名上可以很清楚的看出3个注解分别对应:持久层(M)、业务层(V)、控制层(C),虽然目前这3个注解和@Component等效,但是Spring在以后的版本中为他们添加特殊的功能。所以如果项目分成了上述三层的话,最后在各个层中使用相应的注解。而@Component是对中立的类进行注解。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值