Spring部分1.声明bean的注解
-
@Component 组件,没有明确的角色
-
@Service 在业务逻辑层使用(service层)
-
@Repository 在数据访问层使用(dao层)
-
@Controller 在展现层使用,控制器的声明(C)
2.注入bean的注解
-
@Autowired:由Spring提供
-
@Inject:由JSR-330提供
-
@Resource:由JSR-250提供
3.java配置类相关注解
-
@Configuration 声明当前类为配置类,相当于xml形式的Spring配置(类上)
-
@Bean 注解在方法上,声明当前方法的返回值为一个bean,替代xml中的方式(方法上)
-
@Configuration 声明当前类为配置类,其中内部组合了@Component注解,表明这个类是一个bean(类上)
-
@ComponentScan 用于对Component进行扫描,相当于xml中的(类上)
-
@WishlyConfiguration 为@Configuration与@ComponentScan的组合注解,可以替代这两个注解
4.切面(AOP)相关注解
-
@After 在方法执行之后执行(方法上)
-
@Before 在方法执行之前执行(方法上)
-
@Around 在方法执行之前与之后执行(方法上)
5.@Bean的属性支持
-
Singleton (单例,一个Spring容器中只有一个bean实例,默认模式),
-
Protetype (每次调用新建一个bean),
-
Request (web项目中,给每个http request新建一个bean),
-
Session (web项目中,给每个http session新建一个bean),
-
GlobalSession(给每一个 global http session新建一个Bean实例)
6.@Value注解
@Value("Michael Jackson")
String name;
@Value("#{systemProperties['os.name']}")
String osName;
@Value("#{ T(java.lang.Math).random() * 100 }")
String randomNumber;
@Value("#{domeClass.name}")
String name;
@Value("classpath:com/hgs/hello/test.txt")
String Resource file;
@Value("http://www.cznovel.com")
Resource url;12
@Value("${book.name}")
String bookName;
book.name=《三体》
@PropertySource("classpath:com/hgs/hello/test/test.propertie")
7.环境切换
8.异步相关
9.定时任务相关
10.@Enable*注解说明
-
@EnableAspectJAutoProxy 开启对AspectJ自动代理的支持
-
@EnableAsync 开启异步方法的支持
-
@EnableScheduling 开启计划任务的支持
-
@EnableWebMvc 开启Web MVC的配置支持
-
@EnableConfigurationProperties 开启对@ConfigurationProperties注解配置Bean的支持
-
@EnableJpaRepositories 开启对SpringData JPA Repository的支持
-
@EnableTransactionManagement 开启注解式事务的支持
-
@EnableTransactionManagement 开启注解式事务的支持
-
@EnableCaching 开启注解式的缓存支持
11.测试相关注解
@RunWith(SpringJUnit4ClassRunner.class)1
@ContextConfiguration(classes={TestConfig.class})1
SpringMVC部分
这对所有注解了 @RequestMapping的控制器内的方法有效。