Spring:IoC 用法(二、自动注入用法)

本文主要介绍了Spring中自动注入的使用,包括定义Bean、使用@Autowired、@Resource和@Inject进行注入,以及如何通过XML配置和Java注解来发现Bean。并提供了完整的配置类和测试类示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自动注入

1、定义Bean

首先了解几个常用注解:

@Controller:控制层注解

@Controller
public class TestController {
    // 测试:被访问的方法
    public void test() {
        System.out.println("@Controller 生效");
    }
}

@Service:服务层注解

@Service
public class TestService {
    // 测试:被访问的方法
    public void test() {
        System.out.println("@Service 生效");
    }
}

@Repository:数据访问层注解

@Repository
public class TestRepository {
    // 测试:被访问的方法
    public void test(){
        System.out.println("@Repository 生效");
    }
}

@Component:组件注解

@Component
public class TestComponent {
    // 测试:被访问的方法
    public void test(){
        System.out.println("@Component 生效");
    }
}

@Named:Java依赖规范注解

@Named
public class TestNamed {
    // 测试:被访问的方法
    public void test(){
        System.out.println("@Named 生效");
    }
}

2、注入Bean

注入Bean动作,其实就是实例化Bean的动作:

@Autowired:根据类型自动注入

@Autowired
private TestController controller;

@Resource:JDK方式注入

@Resource
private TestService service;

@Inject:Java依赖注入规范

@Inject
private TestRepository repository;

3、发现Bean

把Bean注册到容器中,通知容器完成各类操作(比如注入):

XML配置发现:扫描所有带注解的Bean(详细用法不在此处说明)

<context:component-scan base-package="test"/>

Java注解发现:@ComponentScan:扫描指定路径

@ComponentScan(basePackages = "test.define")

4、完整测试类

Java完整配置类

@Configuration
@ComponentScan(basePackages = "test.define")
public class TestConfig {
}
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">
    
    <context:component-scan base-package="test"/>
</beans>

依赖

<properties>
	<spring.version>4.2.1.RELEASE</spring.version>
	<!--<java.version>1.7</java.version>-->
</properties>

<dependencies>
	<!-- Spring 依赖注入 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<!-- Java依赖注入规范 -->
	<dependency>
		<groupId>javax.inject</groupId>
		<artifactId>javax.inject</artifactId>
		<version>1</version>
	</dependency>
	<!-- 仅测试 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
	</dependency>
</dependencies>

测试类

@RunWith(SpringJUnit4ClassRunner.class)
/* Java 配置启动 */
@ContextConfiguration(classes = TestConfig.class)
/* XML 配置启动 */
//@ContextConfiguration("classpath*:spring/spring.xml")
public class Test {

    @Autowired
    private TestController controller;

    @Resource
    private TestService service;

    @Inject
    private TestRepository repository;

    @Autowired
    private TestComponent component;

    @Autowired
    private TestNamed named;

    @org.junit.Test
    public void test(){
        controller.test();
        service.test();
        repository.test();
        component.test();
        named.test();
    }
}

结果

@Controller 生效
@Service 生效
@Repository 生效
@Component 生效
@Named 生效



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值