Spring注解开发笔记
Spring配置数据源
数据源(连接池)的作用
1.数据源是为提高程序性能而出现的
2.事先实例化数据源,初始化部分连接资源
3.使用连接资源时从数据源中获取
4.使用完毕后将链接资源归还给数据源
druid数据源的创建
pom.xml配置文件:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
测试:
@Test
public void test1() throws SQLException {
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("root");
DruidPooledConnection connection=dataSource.getConnection();
System.out.println(connection);
connection.close();
}
数据源的创建(加载配置文件)
jdbc.properties配置文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
测试:
@Test
public void test3() throws PropertyVetoException, SQLException {
//读取配置文件
ResourceBundle rb=ResourceBundle.getBundle("jdbc");
String driver=rb.getString("jdbc.driver");
String url=rb.getString("jdbc.url");
String username=rb.getString("jdbc.username");
String password=rb.getString("jdbc.password");
//创建数据源对象,设置连接参数
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
Spring配置数据源
pom.xml配置文件:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
applicationContext配置文件:
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="Url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
测试:
@Test
public void test5() throws SQLException {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource=app.getBean(DataSource.class);
Connection connection=dataSource.getConnection();
System.out.println(connection);
connection.close();
}
Spring加载
applicationContext.xml配置文件:
<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:property-placeholder location="classpath:jdbc.properties"/> //加载外部的properties文件
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
@Test
public void test5() throws SQLException {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource=app.getBean(DataSource.class);
Connection connection=dataSource.getConnection();
System.out.println(connection);
connection.close();
}
Spring原始注解
@Component:使用在类上用于实例化Bean
@Component("userService")
@Controller:使用在web层类上用于实例化Bean
@Service:使用在service层类上用于实例化Bean
@Service("userService") //相当于@Component,可读性更强
@Repository:使用在dao层类上用于实例化Bean
@Repository("userDao") //相当于@Component,可读性更强
@Autowired:使用在字段上用于根据类型依赖注入
@Qualifier:结合@Autowired一起使用,用于根据名称进行依赖注入
@Qualifier("userDao")
@Resource:相当于@Autowired+@Qualifier,按照名称进行注入
@Resource(name="userDao") //相当于@Qualifier+@Autowired
@Value:注入普通属性
@Value("${jdbc.username}")
private String username;
@Scope:标注Bean的作用范围
@Scope("prototype") //或者singleton(单个)
@PostConstruct:标注该方法时Bean的初始化方法
@PreDestroy:标注该方法是Bean的销毁方法
Spring新注解
@Configuration:用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注解
@ComponentScan:用于指定Spring在初始化容器时要扫描的包
@ComponentScan("com.it") //代替<context:component-scan base-package="com.it"/>
@Bean:使用在方法上,标注将该方法的返回值存储到Spring容器中
@Bean("dataSource")
@PropertySource:用于加载.properties文件中的配置
@PropertySource("classpath:jdbc.properties")
@Import:用于导入其他配置类
@Import({DataSourceConfiguration.class}) //括号内为数组
Spring集成Junit
步骤:
1.导入Spring集成Junit的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2.使用@Runwith注解替换原来的运行期
@RunWith(SpringJUnit4ClassRunner.class)
3.使用@ContextConfiguration指定配置文件或配置类
@ContextConfiguration("classpath:applicationContext.xml")
4.使用@Autowired注入需要测试的对象
@Autowired
private UserService userService;
5.创建测试方法进行测试
@Test
public void test1() throws SQLException {
userService.save();
}