spring使用注解开发

使用注解要导入context的约束,新加一个依赖。如下:

<dependency>
    <
groupId>javax.annotation</groupId>
    <
artifactId>javax.annotation-api</artifactId>
    <
version>1.3.2</version>
</
dependency>

注解配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      
xmlns:context="http://www.springframework.org/schema/context"
      
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
>
//
开启注解支持

<context:annotation-config/>

//指定要扫描的包
    <context:component-scan base-package="beans"/>

</
beans>

@Component注解等价于《bean  id=   class=   》对象名就是默认类名小写

在xml文件中创建对象

他是放在类上面,说明这个类被spring给管理了

衍生注解:@Resposity   @Service  @Contyoller  只是作用的层不一样

@value直接作用在属性上面,等价于:,《property   》

小结:

xml和注解的区别:

xml更加万能,适用于任何场合,维护更简单,

注解不是自己的实现不了,维护相对复杂,

xml和注解最佳实现:

xml用来管理bean

注解只负责完成属性的注入,

我们在使用的时候,只需要注意一个问题:必须让注解生效,就需要开启注解的支持。

最后一种:java配置类

@value  @Bean

import com.mchange.v2.c3p0.ComboPooledDataSource;

import org.apache.commons.dbutils.QueryRunner;

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 javax.sql.DataSource;



/*该类是一个配置类,他的作用和beanxml是一样的

* spring的新注解

* ConFiguration

* 作用:指定当前类是一个配置类

* 细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写,

*

* ComponentScan

* 作用:用于通过注解指定Spring在创建容器时要烧苗的包

* 属性;value:他和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包

* 使用这个注解就相当于在xml中配置了:

*     <context:component-scan base-package="com.zhagn"></context:component-scan>

*Bean :作用:用于把当前方法的返回值作为bean对象存入SpringIOC容器中

*属性:name:用于指定beanid,当不写时,默认值时当前方法的名称

*细节:

* 当我们使用注解配置方法的时候,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象

* 查找的方式和Autowired注解的作用是一样的。

*import:作用,用于导入其他的配置类,

属性:value:用于指定其他配置类的字节码,当我们使用import的注解后,有import的类就是父类配置,而导入法人都是之类配置

*

* PropertySource

* 作用:用于指定properties文件的位置

* 属性:value:指定文件的名称和路径

* 关键字:classspath,表示类路径下

*

* */

@Configuration

@ComponentScan(basePackages = "com.zhagn")

@PropertySource("classpath:JdbcConfig.properties")

public class SpringConfiguration {

    @Value("${jdbc.driver}")

    private String driver;

    @Value("${jdbc.url}")

    private String url;

    @Value("${jdbc.username}")

    private String username;

    @Value("${jdbc.password}")

    private String password;



    /**

     * 用于创建一个QueryRunner对象

     *

     * @param dataSource

     * @return

     */

    @Bean(name = "runner")

    public QueryRunner createQueryRanner(DataSource dataSource) {

        return new QueryRunner(dataSource);

    }



    @Bean(name = "dataSource")

    public DataSource createDataSource() {

        try {

            ComboPooledDataSource ds = new ComboPooledDataSource();

//            ds.setDriverClass("com.mysql.jdbc.Driver");

//            ds.setJdbcUrl("jdbc:mysql://localhost:3306/db4");

//            ds.setUser("root");

//            ds.setPassword("root");

            ds.setDriverClass(driver);

            ds.setJdbcUrl(url);

            ds.setUser(username);

            ds.setPassword(password);

            return ds;

        } catch (Exception e) {

            throw new RuntimeException(e);

        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值