@Configuration+@Bean
示例代码:
@Configuration
@ComponentScan(basePackages = WebConfig.PACKAGE_NAMESPACE)
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
<span style="white-space:pre"> </span>....................
@Bean
public CommonsMultipartResolver multipartResolver() {
final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setMaxUploadSize(MAX_UPLOAD_SIZE);
return commonsMultipartResolver;
}
@Bean
public TemplateEngine templateEngine() {
final SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(this.templateResolver());
return engine;
}
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource getMessageSource() {
final ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
resource.setBasenames("classpath:i18n/application-messages");
resource.setDefaultEncoding("UTF-8");
return resource;
}
<span style="white-space:pre"> </span>................................
}
@Configuration相当于<beans>标签,所以此处WebConfig.class相当于一个xml配置文件而不是一个bean
@Bean相当于<bean>标签,但我的理解是,它所描述的bean只能是没有引入其他类的简单的bean
@Component
示例代码:
@Service
@Transactional
public class XXServiceImpl implements XXService{
@Autowired
TerminalRepository terminalRepository;
@Autowired
ProductRepository productRepository;
...........
@Component(@Controller @Service @Repository) 也相当于<bean>标签,但我的理解是,他们所描述的不是像@Bean那样简单的bean
@Resource/@autowired/@Inject
前面的都是注入的注解,相当于“存”,而这三个就相当于“取”