Spring 创建bean的三种方式

测试类

package com.kang;

import org.springframework.beans.factory.annotation.Value;
public class People {
    @Value("chengkangValue")
    private String name;
    public People() {

    }

    public People(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

1.xml配置文件

在resource下新建xml配置文件,创建管理bean

<?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">
    <bean id = "people" class="com.kang.People">
        <property name="name" value="chenkang"></property>
    </bean>
</beans>

测试

package com.kang;

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

public class Test1 {
    public static void main(String[] args) {
       //获得bean上下文
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans1.xml");
        //得到bean
        People people = classPathXmlApplicationContext.getBean("people",People.class);
        System.out.println(people.toString());
//
//        ApplicationContext config = new AnnotationConfigApplicationContext(JavaConfig.class);
//        People getPeople = config.getBean("getPeople",People.class);
//        System.out.println(getPeople.toString());
    }
}

二:javaConfig创建管理bean,摒弃xml配置,目前比较流行
需注意java配置中方法名是bean名称

package com.kang;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration("javaConfigBean")
public class JavaConfig {
    @Bean
    public People getPeople() {
        return new People();
    }
}
package com.kang;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {
    public static void main(String[] args) {
//        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans1.xml");
//        People people = classPathXmlApplicationContext.getBean("people",People.class);
//        System.out.println(people.toString());

        ApplicationContext config = new AnnotationConfigApplicationContext(JavaConfig.class);
        People getPeople = config.getBean("getPeople",People.class);
        System.out.println(getPeople.toString());
    }
}

三 纯注解创建bean
在类上标注@Component注解,表示这是一个bean,
@Controller @Service @Respository是其衍生注解,主要标注controller,service和dao层

package com.kang;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class People {
    @Value("chengkangValue")
    private String name;
    public People() {

    }

    public People(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

调用时用 @Autowired 扫描 ,这个是按类型扫描
当类型有多个时,加 @Qualifier(“people”),可以按名称寻找
@Resource是java注解,可以理解为@Autowired和@Qualifier的结合

package com.kang;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {
    @Autowired
    @Qualifier("people")
    private People people;
    public void test1(){
        people.getName();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值