spring03 使用注解的方式 配置bean和实现依赖的注入及整合Junite测试环境

本文介绍了如何使用注解的方式配置Spring Bean和实现依赖注入,详细讲解了在`applicationContext.xml`中配置`component-scan`以及在类中使用注解的方法。同时,内容还涉及了Spring与JUnit的测试环境整合。此外,文章简单阐述了AOP思想,将其比喻为对纵向逻辑的抽取,类似于Filter和Interceptor。

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

 

一,使用注解配置Spring

1.在applicationContext.xml配置文件中配置

<context : component-scan base-package = "cn.itcast.beam."></context>

<!--指定扫描的那个包下的所有的类   -->
	<context:component-scan base-package="cn.itcast.beam.User"></context:component-scan>

2.再类中使用注解完成配置

package cn.itcast.beam;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

//本质上都是一样的,但是为了更加符合我们的思维习惯,javaee中三层架构的思想
//对象类型的注入
@Component("user")
@Service("user")
@Controller("user")
@Repository("user")
// 指定对象的作用范围 其中 sington表示单例模式 prototype表示多例模式
@Scope(scopeName = "prototype")

public class User {
	// 值类型的注入 通过反射的filed赋值 破坏了封装性
	// @Value("tom")
	private String name;
	@Value("18")
	private Integer age;
	// 引用类型的注入方式 第一种自动装配 第二种 手动注入 推荐使用手动注入
	// 1.自动装配 如果有多个对象 不知道注入哪一个对象
	@Autowired
	@Qualifier("car2") // 使用这个属性高速spring容器自动装配那个
	private Car car;
	// 2.手动注入
	@Resource(name = "car")
	private Car car2;

	public String getName() {
		return name;
	}

	// 这个时作用自方法上 使用set方法赋值
	@Value("tom")
	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}

}
package cn.itcast.beam;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//帮我们创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//指定容器使用那个配置文件
@ContextConfiguration("claspath:applicationContext.xml")
public class demo {
//	将名为user的对象注入到u变量中
	@Resource(name="user")
	private User u;
	@Test
	public void fun2(){
		System.out.println(u);
	}
	
	
	
	
	
	/*@Test
	public void fun1() {
		// 创建一个容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 想容器要user对象
		User u = (User) ac.getBean("user");
		System.out.println(u);
	}
*/
	
}

上边的内容是spring整合junite测试 

简单的介绍一下AOP 思想  我自己理解的就是对纵向的一种抽取,向我们以前学习的filter intercetor 过滤器拦截器是一样的 如下图所示 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值