<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注入属性,利用set方法注入-->
<!-- <bean id="user" class="com.llq.User">-->
<!-- <property name="userName" value="llq"></property>-->
<!-- </bean>-->
<!-- 注入属性,利用构造器注入-->
<!-- <bean id="user" class="com.llq.User">-->
<!-- <constructor-arg name="userName" value="llq"></constructor-arg>-->
<!-- <constructor-arg name="age" value="23"></constructor-arg>-->
<!-- </bean>-->
<!-- 注入属性,(null与特殊符号)-->
<!-- <bean id="user" class="com.llq.User">-->
<!-- <property name="userName" >-->
<!-- <value><![CDATA[<<零零七>>]]></value>-->
<!-- </property>-->
<!-- <property name="age"><null></null></property>-->
<!-- </bean>-->
<!-- <bean id="userService" class="com.llq.UserService">-->
<!-- <property name="userDao" ref="userDaoImpl"></property>外部bean的使用-->
<!-- </bean>-->
<!-- <bean id="userDaoImpl" class="com.llq.UserDaoImpl"></bean>-->
<!--内部bean操作
<bean id="employee" class="com.llq.Employee">
<!–设置俩个普通属性–>
<property name="name" value="小明"></property>
<property name="gender" value="女"></property>
<!–设置对象类型属性–>
<property name="department">
<bean id="dep" class="com.llq.Department">
<property name="dName" value="保安部"></property>
</bean>
</property>
</bean>-->
<!--<bean id="student" class="com.llq.Student">
<!–数组属性注入–>
<property name="course">
<array>
<value>数学</value>
<value>语文</value>
</array>
</property>
<!–list属性注入–>
<property name="list">
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<!–map属性注入–>
<property name="maps">
<map>
<entry key="llq" value="fym"></entry>
<entry key="fym" value="llq"></entry>
</map>
</property>
<!–set属性注入–>
<property name="set">
<set>
<value>llq love fym</value>
</set>
</property>
<!–List中注入对象属性–>
<property name="courseList" >
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<bean id="course1" class="com.llq.Course">
<property name="cname" value="数据结构"></property>
</bean>
<bean id="course2" class="com.llq.Course">
<property name="cname" value="操作系统"></property>
</bean>-->
<!--测试工厂bean
<bean id="myBean" class="com.llq.MyBean">-->
<!--测试单实例and多实例,singleton为单实例,prototype为多实例
<bean id="course" class="com.llq.Course" scope="prototype"></bean>
<bean id="course" class="com.llq.Course" scope="singleton"></bean>-->
</bean>
</beans>
各大类
package com.llq;
/*
部门类
*/
public class Department {
private String dName;
public void setdName(String dName) {
this.dName = dName;
}
@Override
public String toString() {
return "Department{" +
"dName='" + dName + '\'' +
'}';
}
}
package com.llq;
/*
员工类
*/
public class Employee {
private String name;
private String gender;
//员工属于某一个部门
private Department department;
public void setDepartment(Department department) {
this.department = department;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", department=" + department +
'}';
}
}
package com.llq;
public class Course {
private String cname;
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}
package com.llq;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student {
//数组类型
private String[] course;
//集合类型
private List<String> list;
//map类型
private Map<String,String> maps;
private Set<String> set;
private List<Course> courseList;
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public void setCourse(String[] course) {
this.course = course;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSet(Set<String> set) {
this.set = set;
}
@Override
public String toString() {
return "Student{" +
"course=" + Arrays.toString(course) +
", list=" + list +
", maps=" + maps +
", set=" + set +
", courseList=" + courseList +
'}';
}
}
package com.llq;
public class User {
private String userName;
private String age;
public void setUserName(String userName) {
this.userName = userName;
}
public void setAge(String age) {
this.age = age;
}
public User() {
}
public User(String userName, String age) {
this.userName = userName;
this.age = age;
}
public void add(){
System.out.println("userName=" + userName);
}
public void show(){
System.out.println(userName + "今年" + age + "岁");
}
}
package com.llq;
import org.springframework.beans.factory.FactoryBean;
//工厂bean,实现FactoryBean接口,设置泛型,并且实现其抽象方法,
public class MyBean implements FactoryBean<Course> {
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("英语");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
package com.llq;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
public void test1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
user.add();
user.show();
}
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.add();
}
@Test
public void test3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
Employee employee = applicationContext.getBean("employee", Employee.class);
System.out.println(employee.toString());
}
@Test
public void test4(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
Student student = applicationContext.getBean("student", Student.class);
System.out.println(student.toString());
}
@Test
public void test5(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
Course course = applicationContext.getBean("myBean", Course.class);//工厂类
System.out.println(course.toString());
}
}