spring新注解

我们需要注意的是注解和注解修饰的类,方法,属性、是连在一起的,当我们引用使用注解修饰的类,我们也使用了注解

 

@componentscan(value = {"包一",“包二”……})  这个表明需要扫描的包

@Configuration(value = “S”)如下所示,表明默认值为当前类,表明讲当前类作为一个配置类

public @interface Configuration {

    /**
     * Explicitly specify the name of the Spring bean definition associated with the
     * {@code @Configuration} class. If left unspecified (the common case), a bean
     * name will be automatically generated.
     * <p>The custom name applies only if the {@code @Configuration} class is picked
     * up via component scanning or supplied directly to an
     * {@link AnnotationConfigApplicationContext}. If the {@code @Configuration} class
     * is registered as a traditional XML bean definition, the name/id of the bean
     * element will take precedence.
     * @return the explicit component name, if any (or empty String otherwise)
     * @see AnnotationBeanNameGenerator
     */
    @AliasFor(annotation = Component.class)
    String value() default "";

@Bean 修饰方法  将当前方法交给spring容器保管

 @Bean(value = "runner")
    public QueryRunner CreateQueryRunner(DataSource dataSource){//大红线表明容器中没有datasource这个对象
        return new QueryRunner(dataSource);
    }

    @Bean(value = "dataSource")//在容器中生成了datasource对象
    public DataSource CreateDataSoerce(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydatabase");
            dataSource.setUser("root");
            dataSource.setPassword("123456");
            return dataSource;

        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }

@PropertySource(classpath:"文件路徑")

 

posted @ 2020-07-22 15:03  七月在野,八月在宇  阅读(132)  评论(0编辑  收藏  举报