栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

通过注解创建对象,给对象的属性赋值,自动注入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过注解创建对象,给对象的属性赋值,自动注入

1.通过注解创建对象

step1:在配置文件中,加入一个标签(组件扫描器的标签)


step2:在某个你想创建这个类对象的类前面加上Componet注解

@Component(value="myStudent")
//表示告诉spring要创建一个Student类的对象,名字叫myStudent,创建出来的对象放在容器中
//作用跟bean标签一摸一样
//也就说等价于:
//这个注解放置的位置在类的上面,表示创建此类的对象
public class Student
{
    private String name;
    private Integer age;
}

这里value可以省略,直接写对象名,即:@Componet=("myStudent")

如果你甚至连对象名都不写了,直接写@Componet,那么就会使用默认对象名称-类名首字母的小写,比如你这个类叫Student,那么这对象就叫student 

测试代码:

        String config= "applicationContext.xml";
        ApplicationContext ctx=new ClassPathXmlApplicationContext(config);
        //上面创建好了一个容器对象

        //下面创建一个Student类对象
        Student student= (Student) ctx.getBean("myStudent");
        System.out.println("student=="+student);

和@Componet 功能相同的,用来创建对象的其他注解:

(1)@Repository:放在dao接口的实现类上面,表示创建dao对象(持久层对象,访问数据库)

(2)@Service:放在业务层接口的实现类上面,表示创建业务层对象

(3)@Controller:放在控制器类的上面,表示创建控制器对象(表示层对象,接受请求,并且把请求的处理结果显示给用户)

2.通过注解给对象的属性赋值

(1)在想要赋值的属性上面输入@Value注解即可(更推荐这种方式,不推荐下面第二种方式)

@Component(value="myStudent")
public class Student
{
    @Value(value="李四")
    private String name;
    
    @Value(value="20")
    private Integer age;
}

(2)也可以在对应的set方法上卖弄输入@Value注解

@Component(value="myStudent")
public class Student
{
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    @Value(value="李四")
    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    @Value(value="20")
    public void setAge(Integer age) {
        this.age = age;
    }
}

(3)@Value注解+外部属性配置文件(properties文件)

在resources这个文件夹下main放一个myconf.properties文件,文件内容:

myname=张三
myage=28

然后在配置文件中:增加一个property-placeholder标签

 

最后在类中的属性上面或者set方法上面:

@Component(value="myStudent")
public class Student
{
    @Value("${myname}")
    private String name;

    @Value("${myage}")
    private Integer age;
}

3.自动注入(分为byType自动注入和byName自动注入)

也是在   “一个对象的属性是另一个对象”   的情境之下:

(1)一个School类,通过@Component注解创建一个对象

@Component("mySchool")
public class School
{
    @Value("北京大学")
    private String name;
    @Value("北京海淀区")
    private String address;

}

(2)一个Student类,通过@Component注解创建一个对象myStudent

@Component(value="myStudent")
public class Student
{

    @Value("张三")
    private String name;

    @Value("20")
    private Integer age;

    @Autowired
    //默认使用byType
    private School school;
}

spring会搜索容器内有没有对象的类型是School的,显然上面的mySchool对象就是School类型的对象,于是就会将mySchool对象作为属性赋值给myStudent的school属性

如果想要使用byName自动注入:

@Component(value="myStudent")
public class Student
{

    @Value("张三")
    private String name;

    @Value("20")
    private Integer age;

    @Autowired
    @Qualifier(value="mySchool")
    private School school;
}

在容器内找,将名字叫作mySchool的对象赋值给myStudent对象的school属性

(3)运行代码:

        String config= "applicationContext.xml";
        ApplicationContext ctx=new ClassPathXmlApplicationContext(config);
        //上面创建好了一个容器对象

        //下面创建一个Student类对象
        Student student= (Student) ctx.getBean("myStudent");

        System.out.println(student.getAge());
        System.out.println(student.getName());
      
        System.out.println(student.getSchool().getAddress());
        System.out.println(student.getSchool().getName());

 注意:还有一个注解@Resource也可以完成自动注入,对对象的属性进行赋值

将@Autowired改为@Resource即可

@Component(value="myStudent")
public class Student
{

    @Value("张三")
    private String name;

    @Value("20")
    private Integer age;

    @Resource
    private School school;
}

比较有意思的是,@Resource注解默认使用byName自动注入,如果找不到需要的对象,就使用byType自动注入

这是jdk1.8带有的注解,如果jdk版本高于1.8,没有这个注解,就需要手工加入一个依赖:


      javax.annotation
      javax.annotation-api
      1.3.2

@Resource注解只使用byName自动注入:@Resource(name="mySchool")

最后总结:我们学习了两种方式(通过bean标签和通过注解的方式)创建对象,给对象的属性赋值,自动注入

如果对象的属性值经常变,那就用配置文件更好,改写方便,其他时候用注解

人们更愿意用注解的方式,注解为主,配置文件为辅

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/952958.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号