首页 > 编程语言 > 详细

spring新注解

时间:2020-07-22 17:06:19      阅读:82      评论:0      收藏:0      [点我收藏+]

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

 

@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:"文件路徑")

技术分享图片

 

spring新注解

原文:https://www.cnblogs.com/guosai1500581464/p/13360777.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!