Spring 常用注解

  使用注解之前要开启自动扫描功能

  其中base-package为需要扫描的包(含子包)。

<context:component-scan base-package="cn.test"/>
@Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。
@Scope注解 作用域
@Lazy(true) 表示延迟初始化
@Service用于标注业务层组件、 
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件。
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Scope用于指定scope作用域的(用在类上)
@PostConstruct用于指定初始化方法(用在方法上)
@PreDestory用于指定销毁方法(用在方法上)
@DependsOn定义Bean初始化及销毁时的顺序
@Primary自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier("personDaoBean") 存在多个实例配合使用
@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@Async异步方法调用,需要添加以下代码
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10"/>
<property name="maxPoolSize" value="300"/>
</bean>
<task:annotation-driven/>  

 @value

在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件 
中的文件,进行键值对的注入,例子如下: 

1 首先在applicationContext.xml中加入: 
   <beans xmlns:util="http://www.springframework.org/schema/util"  
    xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">  
</beans>  的命名空间,然后 

2 <util:properties id="settings" location="WEB-INF/classes/META-INF/spring/test.properties" />  

3 创建test.properties  内容abc=123 

4 
import org.springframework.beans.factory.annotation.Value;   
import org.springframework.stereotype.Controller;   
import org.springframework.web.bind.annotation.RequestMapping;   
  
@RequestMapping("/admin/images")   
@Controller   
public class ImageAdminController {   
 
  @Value("#{settings['abc']}")
  private String imageDir;   
  @Value("/WEB-INF/test.properties")
  private File testProperties;
} 
这样就将test.abc的值注入了imageDir中了,文件也注入到testProperties了
 
原文地址:https://www.cnblogs.com/cxyj/p/3906442.html