Spring中的注解的使用

1.常用注解

  1. 添加在类上
    @Component:表示创建一个对象,放入IoC容器中。
    @Repository:dao层实现类注解。
    @Service:service层实现类注解。
    @Controller:controller层实现类注解。
    @Scope(scopeName="singleton"):表示单例对象。
    @Scope(scopeName="prototype"):表示多例对象。
    @Configuration:相当于把该类作为xml配置文件中的<beans/>。
    @ComponentScan:注解扫描,代替xml配置文件中的<context:component-scan/>。
    @PropertySource:加载properties配置文件,代替<context:property-placeholader/>。
    @Import:加载另外的配置类,代替<import/>。
    @EnableAspectJAutoProxy:开启AOP注解功能,代替<aop:aspectj-autoproxy/>。
  2. 添加在属性上
    @Value("属性值"):属性是String类型或基本数据类型时使用。
    @Autowired:属性是对象,默认按类型装配。
    @Qualifier("bean name"):一个接口类型同时有两个实现类,使用@Autowired会报错,通过@Qualifier指定bean。
    @Resource:属性是对象,javax扩展包的注解,默认按名称装配。
  3. 添加在方法上
    @PostConstruct:等价于init-method属性。
    @PreDestroy:等价于destroy-method属性。
    @Bean:主要用在@Comfiguration注解的类里。作用是把方法的返回结果放入IoC容器中。

2.@Value的使用

dao接口

public interface CustomerDao {
}

dao实现类

@Repository
public class CustomerDaoImpl implements CustomerDao {
    @Value("${driver}")
    private String driver;
    @Value("${url}")
    private String url;
    @Value("${username}")
    private String username;
    @Value("${password}")
    private String password;

    @Override
    public String toString() {
        return "CustomerDaoImpl{" + "driver='" + driver + '\'' + ", url='" + url + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + '}';
    }
}

properties文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456

配置文件

    <!--引入jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--开启注解扫描-->
    <context:component-scan base-package="com.zhy.dao"></context:component-scan>

测试类

    @Test
    public void test1() {
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取bean
        CustomerDaoImpl customerDaoImpl = context.getBean("customerDaoImpl", CustomerDaoImpl.class);
        System.out.println(customerDaoImpl);
    }

运行结果


运行结果.PNG

3.@Autowired结合@Qualifier的使用

dao接口

public interface CustomerDao {
    void show();
}

dao接口实现类

@Repository
public class CustomerMySqlDaoImpl implements CustomerDao {

    @Override
    public void show() {
        System.out.println("CustomerMySqlDaoImpl的show()");
    }
}
@Repository
public class CustomerOracleDaoImpl implements CustomerDao{
    @Override
    public void show() {
        System.out.println("customerOracleDaoImpl的show()");
    }
}

service接口

@Service
public interface CustomerService {
    void show();
}

service接口实现类

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    @Qualifier("customerOracleDaoImpl")
    private CustomerDao customerDao;

    @Override
    public void show() {
        System.out.println("customerServiceImpl的show()");
        customerDao.show();
    }
}

配置文件

<!--开启注解扫描-->
    <context:component-scan base-package="com.zhy"></context:component-scan>

测试类

    @Test
    public void test1() {
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取bean
        CustomerServiceImpl customerServiceImpl = context.getBean("customerServiceImpl", CustomerServiceImpl.class);
        customerServiceImpl.show();
    }

运行结果


运行结果.PNG

如果不使用@Qualifer指定bean,则会报错。


异常.PNG

4.@Resource的使用

@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource(name = "customerMySqlDaoImpl")
    private CustomerDao customerDao;

    @Override
    public void show() {
        System.out.println("customerServiceImpl的show()");
        customerDao.show();
    }
}

5.@Configuration的使用

配置类

@Configuration
public class SpringConfig {
    public SpringConfig(){
        System.out.println("IoC容器初始化成功");
    }
}

测试类

    @Test
    public void test1() {
        //1.加载配置文件
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    }

运行结果


运行结果.PNG

6.@ComponentScan的使用

dao接口

public interface CustomerDao {
    void show();
}

dao实现类

@Repository
public class CustomerMySqlDaoImpl implements CustomerDao {

    @Override
    public void show() {
        System.out.println("CustomerMySqlDaoImpl的show()");
    }
}
@Repository
public class CustomerOracleDaoImpl implements CustomerDao{
    @Override
    public void show() {
        System.out.println("customerOracleDaoImpl的show()");
    }
}

service接口

@Service
public interface CustomerService {
    void show();
}

service实现类

@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource(name = "customerMySqlDaoImpl")
    private CustomerDao customerDao;

    @Override
    public void show() {
        System.out.println("customerServiceImpl的show()");
        customerDao.show();
    }
}

配置类

@Configuration
@ComponentScan(basePackages = {"com.zhy"})
public class SpringConfig {
    public SpringConfig(){
        System.out.println("IoC容器初始化成功");
    }
}

测试类

    @Test
    public void test1() {
        //1.加载配置文件
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        //2.获取bean
        CustomerServiceImpl customerServiceImpl = context.getBean("customerServiceImpl", CustomerServiceImpl.class);
        customerServiceImpl.show();
    }

运行结果


运行结果.PNG

7.@PropertySource的使用

dao接口

public interface CustomerDao {
    
}

dao实现类

@Repository
public class CustomerDaoImpl implements CustomerDao {
    @Value("${driver}")
    private String driver;
    @Value("${url}")
    private String url;
    @Value("${username}")
    private String username;
    @Value("${password}")
    private String password;

    @Override
    public String toString() {
        return "CustomerDaoImpl{" + "driver='" + driver + '\'' + ", url='" + url + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + '}';
    }
}

配置类

@Configuration
@ComponentScan(basePackages = {"com.zhy"})
@PropertySource(value = "classpath:jdbc.properties")
public class SpringConfig {
    public SpringConfig(){
        System.out.println("IoC容器初始化成功");
    }
}

测试类

    @Test
    public void test1() {
        //1.加载配置文件
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        //2.获取bean
        CustomerDaoImpl customerDaoImpl = context.getBean("customerDaoImpl", CustomerDaoImpl.class);
        System.out.println(customerDaoImpl);
    }

运行结果


运行结果.PNG

8.@Import的使用

JdbcConfig类

@PropertySource(value = "classpath:jdbc.properties")
public class JdbcConfig {
}

配置类

@Configuration
@ComponentScan(basePackages = {"com.zhy"})
@Import(value = JdbcConfig.class)
public class SpringConfig {
    public SpringConfig(){
        System.out.println("IoC容器初始化成功");
    }
}

9.@Bean的使用

dao接口

public interface CustomerDao {
    void show();
}

dao实现类

public class CustomerDaoImpl implements CustomerDao {

    @Override
    public void show() {
        System.out.println("show()");
    }
}

配置类

@Configuration
public class SpringConfig {
    public SpringConfig(){
        System.out.println("IoC容器初始化成功");
    }

    @Bean(name = "customerDao")
    public CustomerDao getCustomerDao(){
        return new CustomerDaoImpl();
    }
}

测试类

    @Test
    public void test1() {
        //1.加载配置文件
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        //2.获取bean
        CustomerDaoImpl customerDaoImpl = context.getBean("customerDao", CustomerDaoImpl.class);
        customerDaoImpl.show();
    }

运行结果


运行结果.PNG
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容