Spring特别篇5-全注解开发

Spring特别篇5-全注解开发_第1张图片

Spring特别篇5-全注解开发_第2张图片

Spring特别篇5-全注解开发_第3张图片

package dowin.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * 完全注解开发
 * */
@Configuration //配置类
@ComponentScan(basePackages = "dowin") //组件扫描
@EnableTransactionManagement //开启事务
@PropertySource("classpath:jdbc.properties")
public class FactoryConfig {
    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.driver}")
    private String driver;

    //创建数据库连接池
    @Bean(name = "dataSource")
    public DruidDataSource getDruidDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setUsername("root");
//        dataSource.setPassword("root");
//        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//        dataSource.setUrl("jdbc:mysql://localhost:3306/db3");
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        return dataSource;
    }

    //创建jdbcTemplate对象
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate getJdbcTemplate(DataSource dataSource){
        //到ioc容器中根据类型找到dataSource
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //注入dataSource
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

}
package testConfig;
import dowin.bean.User;
import dowin.bean2.impl.Student;
import dowin.config.FactoryConfig;
import dowin.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {FactoryConfig.class})
public class testConfig {
    @Autowired
    Student stu;

    @Autowired
    UserService userService;

    @Test
    public void test1(){
//        ApplicationContext app = new AnnotationConfigApplicationContext(FactoryConfig.class);
//        Student stu = app.getBean("student",Student.class);
        stu.doHomeWork();
        List list = userService.findAll();
        System.out.println(list);
    }
}

 

 

你可能感兴趣的:(Spring,spring)