spring-学习笔记-注解方式

//<bean id="car" class="xxx">
@Component  
public class Car {

    @Value("BMW") 
    private String brand;

    @Value("87.9")
    private double price;
    
}

@Component 声明一个spring组件 相当于(bean)

@Value 给一个属性进行赋值

@Component
public class Person {

    @Value("Tom")
    private String name;

    //从容其中找到car类型的bean 然后注入进来,没有会报错
    @Autowired
    private Car car;
    
}

@Autowired 从容器中找到car类型的bean 然后注入进来没有会报错

/**
 * 声明该类为配置类    作用等同于配置文件
 */
@Configuration
/**
 * 去指定包下寻找组件,默认扫描当前包内
 * 扫描不光是带有@Component注解的类 还包括@Service @Repository
 */
@ComponentScan(basePackages = "com.java12.spring.annotation")
public class ComConfig {


}

@Configuration 声明一个类为配置类 等同于配置文件的作用

@ComponentScan(basePackages = "xxx") 去扫描指定的包  包括@Component,@Service,@Repository 注解的类

@Repository 把数据访问层声明为一个bean 可以通过Repository注解的value属性 限定bean的名字

@Service 把Service层声明一个bean

//RunWith使用Spring运行环境测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ComConfig.class)
public class SpringAnnotationTest {

    @Autowired
    private Person person;

    @Autowired
    private PersonService personService;

        @Test
    public void testAuto(){
            System.out.println(person);
        }

}

@RunWith(SpringJUnit4ClassRunner.class) 使用spring的运行测试环境
@ContextConfiguration(classes = ComConfig.class) 指定加载哪个配置文件

@Repository
@Qualifier("userdao")
//可以通过Repository注解的value属性 限定bean的名字
//方便在注入的时候通过Qualifier指定
//或者可以使用@Qualifier 直接限定该Repository的名字
public class UserDaoMysqlImpl implements Userdao{

    @Override
    public void getUserById(String id){
        System.out.println("mysql");
    }

}

@Repository
@Qualifier("userdao") 配合使用

//截止到目前 声明bean的方式有两种
//1.在xml中配置bean节点
//2.将组件声明为@Component @Repository @Service
//3.@Bean 采用java的形式进行声明 new xxx();

@Configuration
@PropertySource("classpath:db.properties") //用于导入properties资源文件 配合Environment使用,使用场景不仅仅数据库
public class DataSourceConfig {


   /* @Autowired
    private Environment environment;

    @Bean
    public PropertySourcesPlaceholderConfigurer config(){
        //加载路径下的资源文件
        Resource classPathResource = new ClassPathResource("db.properties");
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        //设置资源文件路径位置
        configurer.setLocation(classPathResource);
        //最终交给spring容器
        return configurer;
    }*/


    @Bean
    @Profile("online")
    public DataSource getDataSourceOnlibe(){
        ComboPooledDataSource datasource = new ComboPooledDataSource();
        try {
            //采用的是占位符的写法
            datasource.setDriverClass("${jdbc.driverClass}");
            datasource.setJdbcUrl("${jdbc.url}");
            datasource.setUser("${jdbc.user}");
            datasource.setPassword("${jdbc.password}");
            System.out.println("ok");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        System.out.println("onlie");
        return datasource;
    }

   /* @Bean
    @Profile("test")
    public DataSource getDateSourceTest(){
        ComboPooledDataSource datasource = new ComboPooledDataSource();
        try {
            datasource.setDriverClass(environment.getProperty("jdbc.driverClass"));
            datasource.setJdbcUrl(environment.getProperty("jdbc.url"));
            datasource.setUser(environment.getProperty("jdbc.user"));
            datasource.setPassword(environment.getProperty("jdbc.password"));

            return datasource;
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
     return null;
    }
*/

    @Bean
    @Profile("prod")
    public DataSource getDateSource(){
        ComboPooledDataSource datasource = new ComboPooledDataSource();
        try {
            datasource.setDriverClass("com.mysql.jdbc.Driver");
            datasource.setJdbcUrl("jdbc:mysql://localhost:3306/house");
            datasource.setUser("root");
            datasource.setPassword("root");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return datasource;
    }
}

@PropertySource("classpath:db.properties") //用于导入properties资源文件 配合Environment使用,使用场景不仅仅数据库

@Profile("online") 表示运行环境
@Bean   在java代码中实例化一个类

猜你喜欢

转载自blog.csdn.net/xiaosuanmiao123/article/details/82390231