@SpringBootApplication的使用

之前用户使用的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。

@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan

@SpringBootApplication  
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


分开解释@Configuration,@EnableAutoConfiguration,@ComponentScan。
1、@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件
<beans>  
<bean id = "car" class="com.test.Car">
<property name="wheel" ref = "wheel"></property>
</bean>
<bean id = "wheel" class="com.test.Wheel"></bean>
</beans>

相当于:
@Configuration  
public class Conf {
@Bean
public Car car() {
Car car = new Car();
car.setWheel(wheel());
return car;
}
@Bean
public Wheel wheel() {
return new Wheel();
}
}


@Configuration的注解类标识这个类可以使用Spring IoC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean

2、@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。

3、@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller

[url]http://blog.csdn.net/u013473691/article/details/52353923[/url]


[size=medium][color=red][b]@Configuration和@Bean的用法和理解[/b][/color][/size]
spring Boot提倡约定优于配置,如何将类的生命周期交给spring
1、第一种自己写的类,Controller,Service。 用@controller @service即可

2、第二种,集成其它框架,比如集成shiro权限框架,集成mybatis分页插件PageHelper,第三方框架的核心类都要交于Spring大管家管理

[color=blue][b]@Configuration可理解为用spring的时候xml里面的<beans>标签[/b][/color]

[color=blue][b]@Bean可理解为用spring的时候xml里面的<bean>标签[/b][/color]

Spring Boot不是spring的加强版,所以@Configuration和@Bean同样可以用在普通的spring项目中,而不是Spring Boot特有的,只是在spring用的时候,注意加上扫包配置

<context:component-scan base-package="com.xxx.xxx" />,普通的spring项目好多注解都需要扫包,才有用,有时候自己注解用的挺6,但不起效果,就要注意这点。

Spring Boot则不需要,主要你保证你的启动Spring Boot main入口,在这些类的上层包就行
[img]https://img-blog.csdn.net/20160725104529279?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center[/img]

就像这样,DemoApplication是启动类,关于启动类的位置放置,在另一篇博客有专门的去分析
@Configuration和@Bean的Demo类
@Configuration    
public class ExampleConfiguration {

@Value("com.mysql.jdbc.Driver")
private String driverClassName;

@Value("jdbc://xxxx.xx.xxx/xx")
private String driverUrl;

@Value("${root}")
private String driverUsername;

@Value("123456")
private String driverPassword;

@Bean(name = "dataSource")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(driverUrl);
dataSource.setUsername(driverUsername);
dataSource.setPassword(driverPassword);
return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}

}


这样,在项目中

@Autowired

private DataSource dataSource;

的时候,这个dataSource就是我们在ExampleConfiguration中配的DataSource

[url]http://www.cnblogs.com/soundcode/p/6477974.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值