annotation的褒贬之说就不管了..技多不压身嘛..再说了配置文件让我很烦啊..
compass也使用annotation了.
就还有web层与business层没有使用了..
spring annotation真好的满足要求..
先看好文:
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/
讲解了spring的annotation
使用相当简单
相关的annotation
---------------属性使用的annotation
@Autowired 这个跟xml中的autowire="byType"一样啦..
对于有多种type的时候.可以使用
@Qualifier("beanName")也指定..
@Resource与@Autowired一样,但是以byName自动注入的.
--------------bean使用的annotation
@Component
@Repository
@Service
@Controller
这个很明了,功能都差不多,只是可以帮助我们对应层使用
这些annotation的使用要在全局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:component-scan base-package="com.baobaotao"/>
</beans>
关键的是
<context:component-scan base-package="com.baobaotao"/>
beans 的 xmlns也要使用最新的..
-----------------------------------------------------------------
在使用annotation的时候,发现了个问题,过去在使用dao的时候,是运用HibernateDaoSupport,需要注入hibernateTemplate
<bean id="dao" abstract="true">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
这是个好方法,在自定义的dao中加上parent属性就可以了..
但annotation中好像没有相应的属性...
这个郁闷了..我使用的是泛型DAO..
发现一篇老的好blog:
http://blog.springsource.com/2007/06/26/so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate/
So should you still use Spring's HibernateTemplate and/or JpaTemplate??
Spring1.0时代,在使用hibernate和spring的时候,都会被HibernateTemplate为我们提供 benefits(资源和事务管理以及把那个“丑陋”的checked exception转换为runtime exception-DataAccessException )而折服,在项目中不由自主、不假思索地使用它和那个经典的callback方法
在spring2.0后,我们可以使用SessionFactory.getCurrentSession()直接操作hibernate api.更何况在更多的情况下,我们都是直接面对的是hibernate api..
Resource management
Since Hibernate 3.0.1 (and in the Java Persistence API from the moment it was first released) it became possible for Spring to manage the underlying resource without you having to go through any of the templates that are available for those technologies. This means that even if you are using the Hibernate API directly (for example through SessionFactory.getCurrentSession()), you will still be using a Spring-managed Hibernate Session. The same holds for an EntityManager obtained through a JPA EntityManagerFactory. This is another reason why you don't have to use Spring's HibernateTemplate anymore to get an integrated experience.
Transaction management
Now that Spring is capable of handling the underlying resources for you without you having to go through the template, Spring will also be able to synchronize the resources with any transaction going on while acquiring the resource. This means the transaction management concern is also addressed without you having to go through a template. Again, this means we do not necessarily have to use Spring's HibernateTemplate anymore.
Error handling
The one and only thing that is not available directly when you are using the plain APIs that come with Hibernate or JPA (i.e. the Hibernate Session or the JPA EntityManager) is the exception translation of technology-specific data access exceptions into the Spring DataAccessException hierarchy. This we can address very easily however as we'll see in a minute
看了上面的话,什么都可以不用担心了..
放弃template吧..得到更大的自由..