Spring 注释

[b]先说一下@Repository、@Service 和 @Controller。[/b]
在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。

在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。 Spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了 @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
default-default-lazy-init="true">
<!-- 使用annotation定义事务-->
<tx:annotation-driventransaction-manager="transactionManager" proxy-target-class="true"/>
<!--使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入base-package为需要扫描的包(含所有子包)-->
<context:component-scanbase-package="com" />
</beans>


@Autowired
可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。

[b]action控制层[/b]


@Controller("proc")
public class ProductsAction {
@Autowired
// @Qualifier("productsService")
private ProductsService productsService;
}

@Controller应用引用的proc就是struts.xml里面定义的namespace

ProductsService该类为业务层接口
[b]业务层[/b]



@Service
@Transactional
public class ProductsServiceImpl implements ProductsService {
@Autowired
private ProductsDao productsDao;
……
}


[b]ProductsDao为持久层接口[/b]


@Repository
public class ProductsDaoImpl implementsProductsDao {
……
}


[b]@Resource[/b]
@Resource 的作用相当于 @Autowired,只不过
@Autowired 按 byType 自动注入,面 @Resource 默认按 byName
自动注入罢了。@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将
@Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用
name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。
Resource 注释类位于 Spring 发布包的 lib/j2ee/common-annotations.jar 类包中,因此在使用之前必须将其加入到项目的类库中。

[b]@PostConstruct 和@PreDestroy[/b]
标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。

[b]@Qualifier[/b]
这里说一下@Qualifier("name")这个方法,这里的name为bean类名

@Service
@Transactional
public class ProductsServiceImpl implements ProductsService {
@Autowired
@Qualifier("prodBuliderImpl")
private TreeBuilder<TabProducts> prodBulider;
……
}


[b]TreeBuilder接口[/b]
public interface TreeBuilder<T> {
……
}


ProdBuliderImpl 类

@Component
public class ProdBuliderImpl implements TreeBuilder<TabProducts>{
……
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值