标签:web项目 ring ado ddr multi auto aspectj public man
xml配置 注解配置 Java配置
常用的:@Component @Service @Repository @Controller @Configuration @Bean
@EnableAspectJAutoProxy @Aspect @PoinCut @After @Before @BeforeThrowing @AtferThrowing @Around
execution(* com..*.* (...))
within(com.text.*)包下任意类
this(com.test.lntf)实现Intf接口的所有类或单个类
@within/target(com.xxx.Transactional)带有Transactional标注的所有类
@annotion(com.xxx.Transactional)带有Transactional标注的所有类的任意方法
@args(com.xxx.Transactional)参数是带有Transactional标注的所有类
args(String)参数类型
@Scope("prototype")或singleton Request Session
注入普通字符
操作系统属性
表达式结果
其他Bean属性
文件内容(Resource)
网址内容(Resource)
属性文件,注入配置文件需要使用@PropertySource指定文件地址
在使用@Value注入要配置PropertySourcesPlaceholderConfigurer的Bean
@Bean(initMethod="init", destroyMethod="destroy")
指定profile
1、设定Environment的ActiveProfies
2、jvm的spring.profiles.active
3、servlet的context parameter
1、自定义事件,继承ApplicationEvent
2、定义事件监听器,实现ApplicationListener
3、使用容器发布事件
BeanNameAware
BeanFactoryAware AppiicationContextAware
MessageSourceAware
ApplicationEventPublisherAware
ResourceLoaderAware
继承上述接口,使用容器提供的功能
此外,可以直接以@Autowired方法向Bean注入
1、配置类使用@EnablbeScheduling
2、方法上使用@Scheduled(fixedRate/fixedDelay/cron)
1、配置类使用@EnableAsync注解开启异步任务支持
2、配置类实现AsyncConfigurer接口并重写getAsyncExecutor方法,返回ThreadPoolTaskExecutor
3、方法上使用@Async,表明该方法是一个异步方法
@RunWith
@ContextConfiguration
@ActiveProfiles
1、配置类使用@EnableWebMvc,可以继承WebMvcConfigureAdapter,重写其方法,完成SpringMVC的配置
2、实现WebApplicationInitializer接口,完成web配置
3、打包处理
public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext arg0) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(MyMvcConfig.class); ctx.setServletContext(arg0); Dynamic servlet = arg0.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); } } @Configuration @EnableWebMvc @ComponentScan public class MyMvcConfig extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/classes/views/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(JstlView.class); return viewResolver; } @Bean public DemoInterceptor demoInterceptor() { return new DemoInterceptor(); } @Bean public MultipartResolver multipartResolver() { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); multipartResolver.setMaxUploadSize(1000000); return multipartResolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/"); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(demoInterceptor()); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/index").setViewName("/index"); registry.addViewController("/toUpload").setViewName("/upload"); } }
@Controller @RequestMapping @ResponseBody @RequestBody @PathVariable @RestController
@ExceptionHandler @ModelAttribute @InitBinder
@RunWith
@ContextConfiguration(MVC配置类)
@WebAppconfiguration(资源文件位置)
@RunWith
@SpringApplicationConfiguration
@WebAppConfiguration
Configuration
EnableAutoConfiguration
ComponentScan
@ConfigurationProperties(prefix = "druid") public class DruidProperties { private String url; ... } @Configuration @EnableConfigurationProperties(DruidProperties.class) @ConditionalOnClass(DruidDataSource.class) @ConditionalOnProperty(prefix = "druid", name = "url") @AutoConfigureBefore(DataSourceAutoConfiguration.class) public class JpaConfig { @Autowired private DruidProperties properties; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(properties.getUrl()); dataSource.setUsername(properties.getUsername()); dataSource.setPassword(properties.getPassword()); if (properties.getInitialSize() > 0) { dataSource.setInitialSize(properties.getInitialSize()); } if (properties.getMinIdle() > 0) { dataSource.setMinIdle(properties.getMinIdle()); } if (properties.getMaxActive() > 0) { dataSource.setMaxActive(properties.getMaxActive()); } dataSource.setTestOnBorrow(properties.isTestOnBorrow()); try { dataSource.init(); } catch (SQLException e) { throw new RuntimeException(e); } return dataSource; } @Bean PlatformTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setDataSource(dataSource()); return transactionManager; } }
标签:web项目 ring ado ddr multi auto aspectj public man
原文地址:http://www.cnblogs.com/m2492565210/p/7898828.html