温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring的注解有哪些

发布时间:2021-12-20 14:55:08 来源:亿速云 阅读:102 作者:iii 栏目:大数据

本篇内容介绍了“Spring的注解有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

####@Configuration  
用于指定当前类是一个 spring 配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration  注解的类.class)。当注解的配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以省略

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);

属性:value:用于指定配置类的字节码
####@ComponentScan  
用于指定 spring 在初始化容器时要扫描的包。作用和在 spring 的 xml 配置文件中的:<context:component-scan base-package="com.xx"/>是一样的。
属性:basePackages:用于指定要扫描的包。和该注解中的 value 属性作用一样。
####@Bean  
该注解只能写在方法上,用于把当前方法的返回值放入spring的IOC容器中。如果注解的方法有参数的时,spring框架回去容器中查找有没有可以匹配的bean对象,与Autowired注解的作用是一样的
属性:name 指定当前bean的id,默认值是当前方法的名称。
xml配置文件

<context:component-scan base-package="com.bx"></context:component-scan>    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">        <constructor-arg name="ds" ref="dataSource">        </constructor-arg>    </bean>    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf-8"></property>        <property name="user" value="root"></property>        <property name="password" value="baxiang"></property>    </bean>

转换成注解

@Configuration@ComponentScan(basePackages = "com.bx")public class SpringConfiguration {    @Bean(name = "runner")    public QueryRunner createQueryRunner(DataSource dataSource){        return new QueryRunner(dataSource);    }    @Bean("datasource")    public DataSource createDataSource(){        try {            ComboPooledDataSource ds = new ComboPooledDataSource();            ds.setDriverClass("com.mysql.jdbc.Driver");            ds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf-8");            ds.setUser("root");            ds.setPassword("baxiang");            return ds;        }catch (Exception e){            e.printStackTrace();        }        return null;    }}

####@PropertySource  
用于加载 .properties 文件中的配置 。配置数据源时,可以把连接数据库的信息写到properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。
属性:value[]:用于指定 properties 文件位置。如果是在类路径下,需要写上 classpath
jdbcConfig.properties

设置@PropertySource注解 需要设置classpath

####@Import  
用于导入其他配置类,在引入其他配置类时,可以省略@Configuration  注解。
属性:value[]:用于指定其他配置类的字节码。当我们使用Import的注解之后,有Import注解的类就是父配置类,而导入的都是子配置类

“Spring的注解有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI