spring 注脚零配置概略(转载)
spring 注解零配置概略(转载)我们在以前学习Spring的时候,其所有的配置信息都写在applicationContext.xml
spring 注解零配置概略(转载)
我们在以前学习Spring的时候,其所有的配置信息都写在applicationContext.xml里,大致示例如下:
<beans><bean name="ds" value="oracle.jdbc.driver.OracleDriver"/><property name="url" value="jdbc:oracle:thin:@localhost:1521:wangbin"/><property name="username" value="tech37"/><property name="password" value="tech37"/></bean> <bean name="txManager" ref="ds"/></bean><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice> <aop:config><aop:advisor advice-ref="txAdvice"pointcut="execution(* cn.javass..business.ebo.*Ebo.*(..))"/></aop:config></beans>
lazy-init=“true” //是否延迟初始化 scope=“prototype” //bean的生命周期 depends-on=“其他bean“ //依赖其他bean /> 在Spring中也有相应的注解去对应 @Lazy @Scope @DependsOn 他们都是放在类的头上。 init-method=“init“ //初始化方法 destroy-method=“close“ //析构方法 /> 在Spring中也有相应的Bean去对应,当然,这两个注解是jdk里内置的 @PostConstruct @PreDestroy 这两个注解放在方法头上。 @Configuration("ctx")public class JavaApplicationContext {@Beanpublic String hello(){return "hello";}@Beanpublic int max(){return 9;}}使用AnnotationConfigApplicationContext获得Spring容器 ApplicationContext context = new AnnotationConfigApplicationContext(JavaApplicationContext.class); 使用@ImportResource (“classpath:applicationContext.xml”)可以把其他容器导入到这个容器中。 Spring使用的AOP注解分为三个层次:1、@Aspect放在类头上,把这个类作为一个切面,但是这个类一定要显式的注册在Spring容器中。 2、 @Pointcut放在方法头上,定义一个可被别的方法引用的切入点表达式。 3、5种通知。 3.1、@Before,前置通知,放在方法头上。 3.2、@After,后置【finally】通知,放在方法头上。 3.3、@AfterReturning,后置【try】通知,放在方法头上,使用returning来引用方法返回值。 3.4、@AfterThrowing,后置【catch】通知,放在方法头上,使用throwing来引用抛出的异常。 3.5、@Around,环绕通知,放在方法头上,这个方法要决定真实的方法是否执行,而且必须有返回值。