spring IoC---注解配置方式的依赖注入

一、spring bean 配置常用的注解

1、声明bean的注解

  • @Component  用于基本组件的注解,没有明确的角色
  • @Repository 用于数据访问层(Dao)的注解
  • @Service 用于业务逻辑层(Service)的注解
  • @Controller 用于表现层(MVC/Spring MVC)的注解

其实对于Spring来说,这四个注解可以混用,只要有这四个注解的其中一个他就帮你创建Bean。之所以按照上面介绍的规则来使用,是为了代码更有可读性。

2、注入Bean的注解

  • @Autowired 按照类型注入(spring提供)
  • @Qualifier 按照名字注入(spring提供,使用这个的时候,在声明bean的时候必须要指定bean的名字,不然找不到)
  • @Resource 默认先按照名字注入,其次按照类型注入(JSR-250提供)
  • @Inject (JSR-330提供)

可注解在set方法或者属性上,一般经常注解在属性上,优点是代码更少,层次更清晰。

二、使用注解(直接上代码)

1、使用注解的时候xml文件不在声明bean,但是要定义扫描包的范围,使spring自动去扫描有注解的并创建bean

配置文件如下:beans-annotation.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-4.1.xsd">
        
    <!-- 
        指定SpringIOC容器扫描的包
        可以使用resource-pattern指定扫描的资源
     -->    
    <!--  
    <context:component-scan base-package="me.spring.beans.annotation" resource-pattern="repository/*.class"></context:component-scan>
    -->
    <!--  
    <context:component-scan 
        base-package="me.spring.beans.annotation" 
        resource-pattern="repository/*.class"
        use-default-filters="false">
    -->
        <!-- context:exclude-filter子节点指定要排除的组件 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        -->
        <!-- include-filter指定包含直接点的组件,需要use-default-filters配合使用 
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        -->
        <!--  
        <context:exclude-filter type="assignable" expression="org.springframework.stereotype.Repository"/>
        <context:include-filter type="assignable" expression="org.springframework.stereotype.Repository"/>
        -->
    <!--  
    </context:component-scan>
    -->
    <context:component-scan base-package="me.spring.beans.annotation"></context:component-scan>
</beans>

2、repository包下:

UserJdbcRepository.java

package me.spring.beans.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository
public class UserJdbcRepository implements UserRepository {

    public void save() {
        // TODO Auto-generated method stub
        System.out.println("UserJdbcRepository's save");
    }

}

UserRepository.java

package me.spring.beans.annotation.repository;

public interface UserRepository {
    
    public void save();
    
}

UserRepositoryImpl

package me.spring.beans.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepositoryImpl implements UserRepository {

    public void save() {
        // TODO Auto-generated method stub
        System.out.println("UserRepositoryImpl'save");
    }

}

3.service包下

UserService.java

package me.spring.beans.annotation.service;

import me.spring.beans.annotation.repository.UserRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    @Autowired
    @Qualifier("userJdbcRepository")
    private UserRepository userRepository;
    
    public void add(){
        System.out.println("UserService's add method");
        userRepository.save();
    }
    
}

4、controller包下

UserController.java

package me.spring.beans.annotation.controller;

import me.spring.beans.annotation.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    
    @Autowired
    private UserService userService;
    
    public void execute(){
        System.out.println("UserController's execute methos");
        userService.add();
    }
}

5、测试类--Main.java

package me.spring.beans.annotation;

import me.spring.beans.annotation.service.UserService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    
    public static void main(String[] args){
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        userService.add();
        
//        AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(DiConfig1.class);
//        UserService userService = acac.getBean(UserService.class);
//        userService.add();
    }
}

猜你喜欢

转载自www.cnblogs.com/chyxhzh/p/10193915.html