关于spring注解的使用问题

hyhssm 2019-03-08 11:55:39
先上代码:
实体类


package com.hyh.spring.pojo;

import org.springframework.stereotype.Component;

@Component("teacher")
public class Teacher {

private String name;

public Teacher() {
super();
System.out.println("Teacher.Teacher()");
}

public Teacher(String name) {
super();
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

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


spring配置文件:applicationContext-anno.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="com.hyh.spring.pojo"></context:component-scan>

</beans>


测试类 1:


package com.hyh.spring.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hyh.spring.pojo.Teacher;

public class TestTeahcer {

//获取容器对象
private static ApplicationContext con;

static{
con = new ClassPathXmlApplicationContext("classpath:applicationContext-anno.xml");
}

@Resource(name="teacher")
private Teacher teacher;

@Test
public void test01(){
//Teacher teacher = con.getBean(Teacher.class);
System.out.println(teacher);
}
}


测试结果


实体类的构造方法已经被调用了,说明spring容器中已经创建对象了
为什么通过注@Resource解获取不到值,只能通过getBean()的方式获取,或者测试类写成下面这样:

测试类2

package com.hyh.spring.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hyh.spring.pojo.Teacher;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class TestTeahcer2 {

@Resource(name="teacher")
private Teacher teacher;

@Test
public void test01(){
System.out.println(teacher);
}
}

这个可以获取到teacher对象
是不是跟这两个注解有关
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")

在网上查了很多也没弄明白,求解





...全文
106 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyhssm 2019-03-09
  • 打赏
  • 举报
回复
引用 1 楼 小小菜鸟肥 的回复:
恩,首先,@Component("teacher")的意思,是将该类作为组件(Bean)添加到容器中,所有只要你运行程序,就会执行构造方法。而你测试1的情况下,虽然你用con实例了容器,所以你只能通过Teacher teacher = con.getBean(Teacher.class)去获取对象是可行的,但是spring有条规则是:只有这个组件是容器中的组件,才能使用容器提供的功能。你的TestTeahcer类并非是是容器组件,所有并没有办法使用 @Resource()注解。 而测试2,TestTeahce类上的注解的作用是让测试在Spring容器环境下执行,也就是说可以获取容器的Bean对象,故注解生效。 个人见解,也许有更好的解释。这里有套Spring boot的教学视频,有兴趣你可以看下 https://blog.csdn.net/chengjia_2017/article/details/81040452
感谢大佬解惑!
小小菜鸟肥 2019-03-09
  • 打赏
  • 举报
回复
恩,首先,@Component("teacher")的意思,是将该类作为组件(Bean)添加到容器中,所有只要你运行程序,就会执行构造方法。而你测试1的情况下,虽然你用con实例了容器,所以你只能通过Teacher teacher = con.getBean(Teacher.class)去获取对象是可行的,但是spring有条规则是:只有这个组件是容器中的组件,才能使用容器提供的功能。你的TestTeahcer类并非是是容器组件,所有并没有办法使用 @Resource()注解。 而测试2,TestTeahce类上的注解的作用是让测试在Spring容器环境下执行,也就是说可以获取容器的Bean对象,故注解生效。 个人见解,也许有更好的解释。这里有套Spring boot的教学视频,有兴趣你可以看下 https://blog.csdn.net/chengjia_2017/article/details/81040452

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧