测试类
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();
}
}