Spring3 annotation zero configuration

When we used to learn Spring, all its configuration information was written in applicationContext.xml, the general example is as follows:

java code:
view copy to clipboard print
<beans>
<bean name="ds" class="org.apache" .commons.dbcp.BasicDataSource">
<property name="driverClassName" 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" class="org.springframework.jdbc .datasource.DataSourceTransactionManager">
<property name="dataSource" 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>


in the example above , we can typically see three functions of Spring:
1. IoC container, such as:
<bean …>
<property…/>
</bean>
2. AOP
<aop:config/>
3. Transaction
<tx:advice/ >
First we learn how to use annotations to construct IoC containers.
Use annotations to register beans with the Spring container. <context:component-scan base-package="cn.javass"/> needs to be registered in applicationContext.xml. indicate cn.
The usage of the above 4 annotations is exactly the same, only the semantic difference.
@Component is the general form of all Spring-managed components. Spring also provides more detailed annotation forms: @Repository , @Service , @Controller , which correspond to data layer beans, business layer beans, and presentation layer beans, respectively.
Among them, @Component is deprecated.
These four annotations can be placed on the head of the class. If you do not specify its value [@Service], the default bean name is the simple class name of this class with a lowercase first letter; if you specify value [@Service("dao") ], use value as the ban name.
Note: What happens if both cn.javass.SampleDao and cn.javass1.SampleDao are only annotated with @Service without specifying value?
In addition to registering beans, you can also control other properties of beans by setting properties on <bean>, such as:
<bean name="" class=""
lazy-init="true" //Whether to delay initialization
scope=" prototype" //bean's life cycle
depends-on="Other beans" //Depends on other beans
/>
There are also corresponding annotations in Spring to correspond to
@Lazy
@Scope
@DependsOn
They are all placed on the header of the class.
In addition to registering beans, you can also use <bean>
<bean name="" class=""
init-method="init" //Initialization method
destroy-method="close" //Destruction method
/>
There are also corresponding beans in Spring to correspond, of course, these two The annotations are the built-in
@PostConstruct
@PreDestroy annotations in the jdk,
which are placed on the method header.
@Autowired looks up from the spring context based on the bean type. We need to learn this annotation from the following aspects:
1. What head can it be placed on?
It can be placed on property, method and constructor headers.
2. According to what injection?
2.1. If the implementation class of an interface is unique in the Spring container, it can be injected correctly only by using @Autowired, such as:
@Autowired
private SampleDao dao;
2.2. If the implementation class of an interface is not unique in the Spring container
2.2. 1. Use @Qualifier to specify the name of the injected bean, such as
@Autowired
@Qualifier(“sampleDaoImpl”)
private SampleDao dao;
2.2.2. Use @Primary to specify which one is the most preferred among multiple beans. Note that it is not followed by Use with @Autowired, but with @Service, such as
@Service @Primary SampleDaoImpl
2.2.3. Use the attribute name, method parameter name, and constructor parameter name to set which Bean to inject, such as
public class SampleDaoImpl implements SampleDao
public class SampleDaoImpl1 implements SampleDao
corresponds to
@Autowired
private SampleDao sampleDaoImpl;
Note: The attribute name is in It must exist after compilation; however, method parameter names and constructor parameter names must specify the corresponding compilation options to be retained.
3. @Autowired has only one option, boolean required() default true; whether it is required, and the default is true. Therefore, if you only use @Autowired, you must be able to inject, otherwise an error will be reported.

@Resource has a similar effect to @Autowired.
Spring also supports the use of @Configuration to use a class as an IoC container. If a @Bean is registered on one of its method headers, it will be used as a Bean in the Spring container.

java code:
view copy to clipboard print
@Configuration("ctx")
public class JavaApplicationContext {
@Bean
public String hello(){
return "hello";
}
@Bean
public int max(){
return 9;
}
}
Use AnnotationConfigApplicationContext to get Spring container
ApplicationContext context = new AnnotationConfigApplicationContext(JavaApplicationContext.class);
Use @ImportResource("classpath:applicationContext.xml") to import other containers into this container .
The AOP annotations used by Spring are divided into three levels:
1. @Aspect is placed on the class header, and the class is used as an aspect, but this class must be explicitly registered in the Spring container.
2. @Pointcut is placed on the method header to define a pointcut expression that can be referenced by other methods.
3.5 kinds of notifications. www.2cto.com
3.1, @Before, pre-notification, on the method header.
3.2, @After, post [finally] notification, on the method header.
3.3, @AfterReturning, post [try] notification, put it on the method header, use returning to refer to the method return value.
3.4, @AfterThrowing, post [catch] notification, put it on the method header, use throwing to refer to the thrown exception.
3.5, @Around, surround notification, placed in the method header, this method determines whether the real method is executed, and must have a return value.
[size=medium]
[/size]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326657651&siteId=291194637