spring的注解开发

spring的注解开发

需要引入依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spring.version}</version>
</dependency>

spring配置文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
	<!--开启spring注解-->
    <context:annotation-config/>
	<!--配置spring自动扫描的包-->
    <context:component-scan base-package="org.lanqiao"/>

    <!--引入外部配置文件-->
    <import resource="spring-jdbc"/>
</beans>

使用的注解:

@Component 组件

添加该注解的类 会被spring 管理 就相当于xml配置中的bean 默认会使用该类的类名的首字母小写 作为默认id

value属性可以配置组件在容器中的id

@Component("eService") == @Component(value = "eService")

@Controller @Service @Repository

他们三个注解都是针对**@Component**的衍生注解,他们的作用及属性都是一模一样的。

他们只不过是提供了更加明确的语义化。

@Controller: 一般用于表现层的注解。

@Service: 一般用于业务层的注解。

@Repository: 一般用于持久层的注解。

@Autowired

@Autowired 属性自动注入 按照类型注入

按照属性的类型来完成注入 只能注入引用类型

@Qualifier(“eDao”)属性自动注入 按照名称注入

需要搭配@Autowired 一起使用

@Resource( name = “eDao”) 当注解由name属性时 则按照名称注入 如果没有name属性 则按照类型注入。( name = “eDao”) 可选

@Value

作用:

注入基本数据类型和 String 类型数据的

属性:

value:用于指定值

@Scope

作用:

指定 bean 的作用范围。

属性:

value:指定范围的值。

取值: singleton prototype request session globalse

在这里插入图片描述

使用全注解方式开发

添加配置类取代配置文件

@Configuration//表明该类为配置类
@ComponentScan(basePackages = "org.lanqiao")//指定扫描的基本包
@Import(JdbcConfig.class)//导入其他的配置类
public class SpringConfig {//主配置类
}
@Configuration
@PropertySource(value = "classpath:jdbc.properties")
public class JdbcConfig {
   @Value("${jdbc.driver}")
   private String driver;
   @Value("${jdbc.url}")
   private String url;
   @Value("${jdbc.username}")
   private String userName;
  @Value("${jdbc.password}")
   private String password;
  private DruidDataSource dataSource ;
  	@Bean(name = "ds")//表明该方法返回一个bean  并将该对象加入到spring容器中
    public DataSource createDataSource(){
          dataSource = new DruidDataSource();
          dataSource.setDriverClassName(driver);
          dataSource.setUrl(url);
          dataSource.setUsername(userName);
          dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public QueryRunner createQueryRunner(DataSource dataSource){
      return new QueryRunner(dataSource);
    }
}
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/lanqiao
jdbc.username=root
jdbc.password=root

测试类

@Test
public  void selectTest() throws SQLException {
    //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    EmpService service = context.getBean("eService",EmpService.class);
    List<Emp> emps = service.findAll();
   for(Emp emp :emps){
       System.out.println(emp);
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值