第一种
<bean id="date" class="java.util.Date"></bean>
<bean id="tianfen" class="com.qfedu.ioc.bean.Tianfen" autowire="byType" >
<property name="title" value="P8"></property>
<property name="date" ref="date">
</property>
</bean>
第二种
<bean id="tianfen" class="com.qfedu.ioc.bean.Tianfen" autowire="byType" >
<property name="title" value="P8"></property>
<property name="date" >
<bean class="java.util.Date"></bean>
</property>
</bean>
自动装配autowire
<bean id="rvrt" class="com.qfedu.ioc.bean.Clazz">
<property name="number" value="3"></property>
</bean>
<bean id="clazzyfyufuf" class="com.qfedu.ioc.bean.Clazz">
<property name="number" value="6"></property>
</bean>
<bean id="student" class="com.qfedu.ioc.bean.Student" autowire="byName" >
<property name="stuNum" value="1"/>
<property name="stuName" value="ygl"/>
<property name="stuAge" value="21"/>
<property name="enterenceTime" ref="date"></property>
</bean>
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Object tianfen = context.getBean("student");
System.out.println(tianfen);
}
}
spring注解配置
修改xml如下
<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">
<?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.qfedu.ioc.bean"></context:component-scan>
<!--
<bean id="student" class="com.qfedu.ioc.bean.Student">
<property name="stuNum" value="1"/>
<property name="stuName" value="ygl"/>
<property name="stuAge" value="21"/>
<property name="enterenceTime" ref="date"></property>
</bean>
-->
<bean id="date" class="java.util.Date"></bean>
<bean id="tianfen" class="com.qfedu.ioc.bean.Tianfen" autowire="byType" >
<property name="title" value="P8"></property>
<property name="date" >
<bean class="java.util.Date"></bean>
</property>
</bean>
</beans>
@Scope
加上@Scope(“prototype”),将其设置为多例模式
@Lazy(false) 类注解,用于声明一个单例模式的Bean是否为懒汉模式,默认为饿汉式
@Lazy(true)表示声明为懒汉模式
测试类
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");
System.out.println("------------------------------------");
Object student = classPathXmlApplicationContext.getBean("student");
System.out.println(student);
}
}
Student类
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component
@Lazy(false)
public class Student {
private int age=111;
private String name="e";
private int dengji=9;
public Student(){
System.out.println("创建");
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDengji() {
return dengji;
}
public void setDengji(int dengji) {
this.dengji = dengji;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", dengji=" + dengji +
'}';
}
}