spring IOC之注解

Java框架

浏览数:75

2019-8-22

AD:资源代下载服务

注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。

1.开发环境搭建

  • 拷贝jar包到工程

所需jar包

  • 在类的根路径下创建一个任意名称的xml文件(不能是中文)
    给配置文件导入约束
    约束在spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html目录下的

约束

bean.xml

<?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">
    
</beans>

然后就可以开始使用注解了

2.常用注解

①用于创建bean对象

  • @Component
    作用:相当于配置了一个bean标签
    出现的位置:类上面
    属性:value。value的值是指定bean的id,当不写,默认值是当前类的短名首字母改小写

还是CustomerServiceImpl.java类

package com.edu.services.Impl;

import com.edu.services.ICustomerService;
import org.springframework.stereotype.Component;


@Component(value = "customerServiceImpl")
public class CustomerServiceImpl implements ICustomerService {

    @Override
    public void saveCustomer() {
        System.out.println("执行了保存用户");
    }
}

配置bean.xml

<?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">

<!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中 -->
    <context:component-scan base-package="com.edu"></context:component-scan>

</beans>

用client.java测试

package com.edu.ui;

import com.edu.services.ICustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args){

        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ICustomerService cs = (ICustomerService) ac.getBean("customerServiceImpl");
        cs.saveCustomer();
    }
}

运行结果

  • @Component注解衍生了一下三个注解,他们的作用及属性都是一模一样的。
    • @Controller
      一般用于表现层的注解。
    • @Service
      一般用于业务层的注解。
    • @Repository
      一般用于持久层的注解。

②用于注入数据

  • @Autowired
    作用:自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他bean类型。当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在spring容器查找,找到了也可以注入成功。找不到就报错。

CustomerServiceImpl.java

package com.edu.services.Impl;

import com.edu.dao.ICustomerDao;
import com.edu.services.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


@Service(value = "customerServiceImpl")
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private ICustomerDao customerDao = null;

    @Override
    public void saveCustomer() {
        System.out.println("业务层调用持久层");
        customerDao.saveCustomer();
    }
}

运行结果

  • @Qualifier
    作用:在自动按照类型注入的基础之上,再按照Bean的id注入。它在给类成员注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入时,可以独立使用。
    属性:value,用于指定bean的id
  • @Resource
    作用:直接按照bean的id注入
    属性:name,用于指定的bean的id

以上三个注解都是用于注入其他的bean类型的。

  • @Value
    作用:用于注入基本类型和String类型。可以借助spring的el表达式读取properties文件中的配置。
    属性:value,用于指定要注入的数据。

CustomerServiceImpl.java

package com.edu.services.Impl;

import com.edu.dao.ICustomerDao;
import com.edu.services.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service(value = "customerServiceImpl")
public class CustomerServiceImpl implements ICustomerService {

    @Value("你是猪")
    private String name;

    @Resource(name = "customerDaoImpl")
    private ICustomerDao customerDao = null;

    @Override
    public void saveCustomer() {
        System.out.println("业务层调用持久层..."+name);
        customerDao.saveCustomer();
    }
}

运行结果

③用于改变作用范围的

  • @Scope
    作用:指定bean的作用范围。
    属性:value,指定范围的值。
    value的取值:singleton prototype request session globalsession

Client.java

package com.edu.ui;

import com.edu.services.ICustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args){

        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ICustomerService cs = (ICustomerService) ac.getBean("customerServiceImpl");
//        cs.saveCustomer();
        ICustomerService cs1 = (ICustomerService) ac.getBean("customerServiceImpl");
        System.out.println(cs == cs1);
    }
}

没改scope之前

运行结果

CustomerServiceImpl.java

package com.edu.services.Impl;

import com.edu.dao.ICustomerDao;
import com.edu.services.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service(value = "customerServiceImpl")
@Scope("prototype")
public class CustomerServiceImpl implements ICustomerService {

    @Value("你是猪")
    private String name;

    @Resource(name = "customerDaoImpl")
    private ICustomerDao customerDao = null;

    @Override
    public void saveCustomer() {
        System.out.println("业务层调用持久层..."+name);
        customerDao.saveCustomer();
    }
}

更改scope之后

IOC的注解已经完了,但是我们发现,即使有spring的注解,也依旧离不开xml配置文件,那么可不可以用注解代替xml配置文件呢,这就是spring的新注解。

3.spring的纯注解配置

  • 项目工程目录

工程结构

  • 用SpringConfiguration.java代替bean.xml
package com.edu.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration//表明当前类是一个配置类
@ComponentScan(basePackages = "com.edu")//配置要扫描的包
public class SpringConfiguration {
}
  • 测试类的Client.java就不能用bean.xml了
package com.edu.ui;

import com.edu.config.SpringConfiguration;
import com.edu.services.ICustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Client {
    public static void main(String[] args) {
        //1.获取容器:由于我们已经没有了xml文件,所以再用读取xml方式就不能用了。
        //这时需要指定加载哪个类上的注解
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        //2.根据bean的id获取对象
        ICustomerService cs = (ICustomerService) ac.getBean("customerService");
        cs.saveCustomer();
    }
}

运行结果

新注解说明

  • @Configuration
    作用:
    用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class)。
    属性:
    value:用于指定配置类的字节码

  • @ComponentScan
    作用:
    用于指定spring在初始化容器时要扫描的包。
    和xml配置文件中的<context:component-scan base-package="com.itheima"/>作用一样
    属性:
    basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。

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

  • @Import
    作用:
    用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration注解。当然,写上也没问题。
    属性:
    value[]:用于指定其他配置类的字节码。

  • @Bean
    作用:
    该注解只能写在方法上,表明使用此方法创建一个对象,并且放入spring容器。它就相当于我们之前在xml配置中介绍的factory-beanfactory-method
    属性:
    name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id)。

运用新注解:

运用纯注解对customer进行增删改查

数据库结构
项目目录结构
需要的jar包

SpringConfiguration.java

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

/**
 * 一个spring的配置类
 *  它的作用就相当于bean.xml
 * @author zhy
 */
@Configuration//它就是把当前类看成是spring的配置类
@ComponentScan({"com.itheima"})
@Import({JdbcConfig.class})//导入其他配置类
@PropertySource("classpath:config/jdbcConfig.properties")
public class SpringConfiguration {
    
    @Bean
    public static PropertySourcesPlaceholderConfigurer createPropertySourcesPlaceholderConfigurer(){
        return  new PropertySourcesPlaceholderConfigurer();
    }
}

JdbcConfig.java

package config;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * Jdbc的配置类
 * @author zhy
 *
 */
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;

    @Bean(name="runner")//它是把方法的返回值存入spring容器中。该注解有一个属性,name:用于指定bean的id。当不指定时它有默认值,默认值是方法的名称。
    public QueryRunner createQueryRunner(@Qualifier("ds1")DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    
    @Bean(name="ds")
    public DataSource createDataSource(){
        try {
            System.out.println(driver);//com.mysql.jdbc.Driver  
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
            return ds;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    @Bean(name="ds1")
    public DataSource createDataSource1(){
        try {
            System.out.println(url);
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
            return ds;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/customer
jdbc.username=root
jdbc.password=root

Customer.java

package com.edu.domain;

import java.io.Serializable;

/**
 * 客户的实体类
 * 我们使用DBUtils来操作。
 * 它的使用要求,实体类中的属性和数据库的字段必须一致
 * @author zhy
 * 
 */
public class Customer implements Serializable {

    private Long cust_id;
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_address;
    private String cust_phone;
    public Long getCust_id() {
        return cust_id;
    }
    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
    public String getCust_name() {
        return cust_name;
    }
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
    public String getCust_source() {
        return cust_source;
    }
    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
    public String getCust_industry() {
        return cust_industry;
    }
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
    public String getCust_level() {
        return cust_level;
    }
    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
    public String getCust_address() {
        return cust_address;
    }
    public void setCust_address(String cust_address) {
        this.cust_address = cust_address;
    }
    public String getCust_phone() {
        return cust_phone;
    }
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
    @Override
    public String toString() {
        return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
                + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_address=" + cust_address
                + ", cust_phone=" + cust_phone + "]";
    }   
}

ICustomerService.java

package com.edu.service;

import java.util.List;

import com.edu.domain.Customer;

/**
 * 客户的业务层接口
 * @author zhy
 *
 */
public interface ICustomerService {
    
    /**
     * 查询所有客户
     * @return
     */
    List<Customer> findAllCustomer();
    
    /**
     * 保存客户
     * @param customer
     */
    void saveCustomer(Customer customer);
    
    /**
     * 更新客户
     * @param customer
     */
    void updateCustomer(Customer customer);
    
    /**
     * 根据id删除客户
     * @param custId
     */
    void deleteCustomer(Long custId);
    
    /**
     * 根据id查询客户
     * @param custId
     * @return
     */
    Customer findCustomerById(Long custId);
}

CustomerServiceImpl.java

package com.edu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.edu.dao.ICustomerDao;
import com.edu.domain.Customer;
import com.edu.service.ICustomerService;
/**
 * 客户的业务层实现类
 * @author zhy
 *
 */
@Service(value = "customerService")
@Scope("prototype")
public class CustomerServiceImpl implements ICustomerService {

    @Resource(name="customerDao")
    private ICustomerDao customerDao ;
    

    @Override
    public List<Customer> findAllCustomer() {
        return customerDao.findAllCustomer();
    }

    @Override
    public void saveCustomer(Customer customer) {
        customerDao.saveCustomer(customer);
    }

    @Override
    public void updateCustomer(Customer customer) {
        customerDao.updateCustomer(customer);
    }

    @Override
    public void deleteCustomer(Long custId) {
        customerDao.deleteCustomer(custId);
    }

    @Override
    public Customer findCustomerById(Long custId) {
        return customerDao.findCustomerById(custId);
    }

}

ICustomerDao.java

package com.edu.dao;

import java.util.List;

import com.edu.domain.Customer;
/**
 * 客户的持久层接口
 * @author zhy
 *
 */
public interface ICustomerDao {
    
    /**
     * 查询所有客户
     * @return
     */
    List<Customer> findAllCustomer();
    
    /**
     *  保存客户
     * @param customer
     */
    void saveCustomer(Customer customer);

    /**
     * 更新客户
     * @param customer
     */
    void updateCustomer(Customer customer);
    
    /**
     * 删除客户
     * @param custId
     */
    void deleteCustomer(Long custId);
    
    /**
     * 根据id查询客户
     * @param custId
     * @return
     */
    Customer findCustomerById(Long custId);
}

CustomerDaoImpl.java

package com.edu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import com.edu.dao.ICustomerDao;
import com.edu.domain.Customer;
/**
 * 客户的持久层实现类
 * @author zhy
 *
 */
@Repository("customerDao")
public class CustomerDaoImpl implements ICustomerDao {

    @Resource(name="runner")
    private QueryRunner runner ;
    

    @Override
    public List<Customer> findAllCustomer() {
        try {
            return runner.query("select * from cst_customer",new BeanListHandler<Customer>(Customer.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveCustomer(Customer customer) {
        try {
            runner.update("insert into cst_customer(cust_name,cust_source,cust_industry,cust_level,cust_address,cust_phone)values(?,?,?,?,?,?)",
                    customer.getCust_name(),customer.getCust_source(),customer.getCust_industry(),
                    customer.getCust_level(),customer.getCust_address(),customer.getCust_phone());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateCustomer(Customer customer) {
        try {
            runner.update("update cst_customer set cust_name=?,cust_source=?,cust_industry=?,cust_level=?,cust_address=?,cust_phone=?  where cust_id=?",
                    customer.getCust_name(),customer.getCust_source(),customer.getCust_industry(),
                    customer.getCust_level(),customer.getCust_address(),customer.getCust_phone(),customer.getCust_id());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteCustomer(Long custId) {
        try {
            runner.update("delete from cst_customer where cust_id = ? ",custId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Customer findCustomerById(Long custId) {
        try {
            return runner.query("select * from cst_customer where cust_id = ? ",new BeanHandler<Customer>(Customer.class),custId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

CustomerServiceTest.java

package com.edu.test;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.edu.domain.Customer;
import com.edu.service.ICustomerService;

import config.SpringConfiguration;
/**
 * 测试客户的业务层
 * @author zhy
 * spring整合junit
 *  第一步:拷贝spring提供的整合jar包
 *      spring-test-4.2.4.RELEASE.jar
 *  第二步:使用junit提供的一个注解,把原有的main函数替换掉,换成spring提供的
 *      @RunWith
 *      要换的类:SpringJunit4ClassRunner
 *  第三步:使用spring提供的的注解告知spring,配置文件或者注解类所在的位置
 *      @ContextConfiguration
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={SpringConfiguration.class})
public class CustomerServiceTest {

    @Autowired
    private ICustomerService cs = null;
    
    @Test
    public void testFindAllCustomer() {
        List<Customer> list = cs.findAllCustomer();
        for(Customer c : list){
            System.out.println(c);
        }
    }

    @Test
    public void testSaveCustomer() {
        Customer c = new Customer();
        c.setCust_name("dbutils customer annotation");
        cs.saveCustomer(c);
    }

    @Test
    public void testUpdateCustomer() {
        Customer c = cs.findCustomerById(94L);
        c.setCust_address("北京市顺义区");
        cs.updateCustomer(c);
    }

    @Test
    public void testDeleteCustomer() {
        cs.deleteCustomer(95L);
    }

    @Test
    public void testFindCustomerById() {
        Customer c = cs.findCustomerById(94L);
        System.out.println(c);
    }

}

运行结果

作者:乐乐亲么么哒