使用注解要导入context的约束,新加一个依赖。如下:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
注解配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
//开启注解支持
<context:annotation-config/>
//指定要扫描的包
<context:component-scan base-package="beans"/>
</beans>
@Component注解等价于《bean id= class= 》对象名就是默认类名小写
在xml文件中创建对象
他是放在类上面,说明这个类被spring给管理了
衍生注解:@Resposity @Service @Contyoller 只是作用的层不一样
@value直接作用在属性上面,等价于:,《property 》
小结:
xml和注解的区别:
xml更加万能,适用于任何场合,维护更简单,
注解不是自己的实现不了,维护相对复杂,
xml和注解最佳实现:
xml用来管理bean
注解只负责完成属性的注入,
我们在使用的时候,只需要注意一个问题:必须让注解生效,就需要开启注解的支持。
最后一种:java配置类
@value @Bean
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
/*该类是一个配置类,他的作用和bean。xml是一样的
* spring的新注解
* ConFiguration
* 作用:指定当前类是一个配置类
* 细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写,
*
* ComponentScan
* 作用:用于通过注解指定Spring在创建容器时要烧苗的包
* 属性;value:他和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包
* 使用这个注解就相当于在xml中配置了:
* <context:component-scan base-package="com.zhagn"></context:component-scan>
*Bean :作用:用于把当前方法的返回值作为bean对象存入Spring的IOC容器中
*属性:name:用于指定bean的id,当不写时,默认值时当前方法的名称
*细节:
* 当我们使用注解配置方法的时候,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象
* 查找的方式和Autowired注解的作用是一样的。
*import:作用,用于导入其他的配置类,
属性:value:用于指定其他配置类的字节码,当我们使用import的注解后,有import的类就是父类配置,而导入法人都是之类配置
*
* PropertySource
* 作用:用于指定properties文件的位置
* 属性:value:指定文件的名称和路径
* 关键字:classspath,表示类路径下
*
* */
@Configuration
@ComponentScan(basePackages = "com.zhagn")
@PropertySource("classpath:JdbcConfig.properties")
public class SpringConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 用于创建一个QueryRunner对象
*
* @param dataSource
* @return
*/
@Bean(name = "runner")
public QueryRunner createQueryRanner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
@Bean(name = "dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
// ds.setDriverClass("com.mysql.jdbc.Driver");
// ds.setJdbcUrl("jdbc:mysql://localhost:3306/db4");
// ds.setUser("root");
// ds.setPassword("root");
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}