(1)null值 解决方案:使用`null`标签 (2)属性值包含特殊符号 解决方案: 1.使用转义字符【可以使用特殊字符转义符号代替】 2.使用`CDATA`区,在`CDATA`区中的数据将原不封不动显示
演示:
1.定义类
package com.haikang.spring.di.dao;
public class UserDao {
private String name;
private String author;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void add(){
System.out.println("userDao add方法");
}
@Override
public String toString() {
return "UserDao{" +
"name='" + name + ''' +
", author='" + author + ''' +
", address='" + address + ''' +
'}';
}
}
2.定义xml文件
>">>"/>
test类
@Test
public void test1(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean2.xml");
UserDao userDao = context.getBean("userDao", UserDao.class);
System.out.println(userDao.toString());
//返回值: UserDao{name='null', author='`<<海康 >>', address='<<湛江>>'}
}
2.注入属性-外部Bean【就是注入对象形式】
在一个类中,需要使用到另一个类中的方法和属性,就需要注入该类的对象
案例
1.创建两个类`Servlet`类和`Dao`类 2.在`Servlet`类中调用`Dao`里面的方法 在`Servlet`类中添加`Dao`属性,并提供`setter`方法或`有参构造器`【演示是使用`setter方法`】 3.在Spring配置文件中进行配置 4.·bean·使用子标签是 property: 表示属性名 ref: 表示引入对象属性名
步骤
第一步:定义UserDao类和UserService类
package com.haikang.spring.di.dao;
public class UserDao {
private String name;
private String author;
private String address;
并且提供`setter`方法和`getter`方法及`toString`方法
public class UserService {
// 由于在UserService层使用到UserDao类方法和属性
// 所以定义UserDao属性
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void service(){
userDao.add();
System.out.println("UserService中的service方法。。。");
}
}
2.创建xml文件
test类
@Test
public void test2(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.service();
}
3.注入集合类型
注入普通属性和对象属性
- 数组类型
- List集合类型
- Set集合类型
- Properties集合
- Map集合类型【需要使用到map标签中的entry标签,在entry中属性key,value , ref属性】
定义类
public class Author {
private String name;
并提供setter方法和getter方法及toString方法
}
public class Book {
// 数组
private String[] arr;
// List集合
private List list;
// Set集合
private Set sets;
// Properties集合
private Properties properties;
// Map集合
Map maps;
并提供setter方法和getter方法及toString方法
}
xml文件
海康 湛江
A_1 B-1 1 2 10086 11001
test类
@Test
public void test(){
ApplicationContext context =
new ClassPathXmlApplicationContext("collection.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
}
4.IOC两种类型bean
- 普通bean
- 工厂bean(FactoryBean)
在配置文件中定义bean类型就是返回类型
如:在bean标签中定义是:Student类型... 在获取Bean对象时,返回就是Student类型 Student student = context.getBean("student", Student.class);
普通bean就是定义的类型和返回类型一致
工厂bean(FactoryBean)实现工厂Bean步骤:
- 创建类,让该类作为工厂bean,需要实现接口FactoryBean
- 实现接口重写方法,在实现的方法中定义返回的bean类型
定义类实现工厂FactoryBean接口
public class MyBean implements FactoryBean{ // gerObject就是定义返回Bean类型 @Override public User getObject() throws Exception { //简化,其实需要读取和解析xml文件,但是演示做了简化 User user = new User(); return user; } @Override public Class> getObjectType() { return null; } }
xml文件
test类
@Test
public void test(){
ApplicationContext context =
new ClassPathXmlApplicationContext("factoryBean1.xml");
User user = context.getBean("myBean", User.class);
System.out.println(user);
// 返回是User类型
//com.haikang.pojo.User@262b2c86
}
可以看出,Bean标签中定义的类型和返回的类型可以不一致,这就是BeanFactory的特点,可以一致,也可以不一致这【如果需要不一致,需要在getObject方法中实现】
5. IOC中Bean作用域1.在Spring中,可以设置创建bean实例是单实例还是多实例
2.在默认情况下Bean是单实例对象
如何设置单实例还是多实例
(1)在Spring配置文件bean标签中有scope属性,用于设置单实例还是多实例
(2)scope属性值
- singleton:表示是单实例对象
- prototype:表示是多实例对象
重点[面试]:singleton和protoype的区别:
- 设置scope值是singleton时,加载Spring配置文件时就会创建单实例对象
- 设置scope值是protoype时,不是加载spring配置文件时创建对象,而在调用getBean方法时才创建对象
public class Person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
xml文件
test类
@Test
public void test(){
ApplicationContext context =
new ClassPathXmlApplicationContext("personBean.xml");
Person person1 = context.getBean("person", Person.class);
Person person2 = context.getBean("person", Person.class);
// 在单实例情况下返回true,在多实例情况下返回false
System.out.println(person1==person2);
}



