spring注解和注释的使用

Spring创建javaBean对象的两种方式:配置文件和注解的方式。
1. spring注解的使用
  1. 添加依赖,如果需要使用spring的注解,需要spring-aop以来(需要导入.jar工具包)
  2. 添加配置文件(常用的是.xml文件),先添加约束文件,再添加配置。配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 设置开启注解 -->
	<context:annotation-config />

	<!-- 设置扫描哪个包下的注解 -->
	<context:component-scan base-package="cn.itcast.spring.demo6" />

</beans>
2. 注释的使用

自己写的java类一般使用注解的方式,系统自带的java类一般用注释的方式

  1. @Component== @Controller == @Service ==@Repository
  2. @Controller(用于数据展现层)
  3. @Service(用于业务逻辑层)
  4. @Repository(用于数据持久层)
  5. @Value(用于Bean中的属性赋值)

编写food:

public class food{
    //给name属性赋值
    @Value("热干面")
    private String fName;
    private int heat;
    //get / set方法
    //toString重写方法
}

编写测试类foodTest:

public class foodTest{
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
        food food = (food) context.getBean("food");
        
        System.out.println(food);
    }
}
spring提供的注解
  1. @Autowired,按照类型注入,注解的字段的类型寻找该类型的实例bean,这种方式称为byType
  2. @Qualifier,按照指定注入的Bean的名字(相当于id)找该类型的实例bean,这种方式称为byName。想要让@Autowired方式按照name方式注入,可以结合@Qualifier(“XX”)使用,让@Autowired按照byName方式装配。
  3. @ReSource,jdk提供的注解,默认是按名称name注入的,也可以按照类型注入。

编写dog:

@Service
public class Dog {
	@Value("拉布拉多")
	private String name;
	private String color;
	private int age;
    //get/set
    //toString
}

编写boyfriend:

@Controller
public class boyfriend {
	@Value("尚某某")
	private String name;
	private int age;
    //get/set
    //toString
}

修改food,使用注解注入dog和boyfriend

@Controller(value="food")
public class food{

    @Autowired
	@Qualifier(value = "dog")
    private dog dog;
    
    @Resource(name = "boyfriend")
    private boyfriend myBF;
    
    @Value("热干面")
    private String name;
    private int heat;
    
    public void hello(){
        System.out.println("My favorite breakfast!!!");
    }
    //get/set
    //toString
}
spring和JUnit整合
  1. 添加依赖
<!-- spring测试组件,其中版本号根据自己的情况写入即可 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>4.2.4.RELEASE</version>
	<scope>test</scope>
</dependency>
  1. 编写测试
//@RunWith设置测试环境,里面的参数指定测试环境
//SpringJUnit4ClassRunner.class:表示使用spring环境

@RunWith(SpringJUnit4ClassRunner.class)

// 设置启动的时候,需要加载的配置文件

@ContextConfiguration(value = "classpath:applicationContext2.xml")
public class AnnoTest2 {

	@Autowired
	private food food;

	@Test
	public void testJunit() {
		System.out.println(food);
		this.food.hello();
	}
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值