1.字面量值
(1)null值
解决方案:使用`null`标签
(2)属性值包含特殊符号
解决方案:
1.使用转义字符【可以使用特殊字符转义符号代替】
2.使用`CDATA`区,在`CDATA`区中的数据将原不封不动显示
演示:
1.定义类
package com.haikang.spring.di.dao;
/**
* @Author 海康
* @Version 1.0
*/
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
文件
<?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">
<!--空值方式-->
<bean id="userDao" class="com.haikang.spring.di.dao.UserDao">
<!--1.空值方式-->
<property name="name">
<null/>
</property>
<!--
特殊字符:
1.转义方式
2.`CDATA`区方式
-->
<property name="author" value="`<<海康 >>"/>
<property name="address" >
<value><![CDATA[湛江]]></value>
</property>
</bean>
</beans>
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;
/**
* @Author 海康
* @Version 1.0
*/
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
文件
<!--注入外部bean方式-->
<!--注入UserDao对象-->
<bean id="userDao" class="com.haikang.spring.di.dao.UserDao">
<property name="name" value="海康"/>
<property name="author" value="湛江"/>
<property name="address" value="西安"/>
</bean>
<!--注入UserService对象-->
<bean id="userService" class="com.haikang.spring.di.service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
test
类
@Test
public void test2(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.service();
/**
* 返回值
* userDao add方法
* 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<String> list;
// Set集合
private Set<Integer> sets;
// Properties集合
private Properties properties;
// Map集合
Map<String,Author> maps;
并提供setter方法和getter方法及toString方法
}
xml
文件
<?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">
<bean id="book" class="com.haikang.spring.di.pojo.Book">
<!--数组类型-->
<property name="arr" >
<array>
<value>海康</value>
<value>湛江</value>
</array>
</property>
<!--List集合-->
<property name="list">
<list>
<value>A_1</value>
<value>B-1</value>
</list>
</property>
<!--Set集合-->
<property name="sets">
<set>
<value>1</value>
<value>2</value>
</set>
</property>
<!--Properties集合-->
<property name="properties">
<props>
<prop key="电话">10086</prop>
<prop key="电话">11001</prop>
</props>
</property>
<!--Map集合-->
<property name="maps">
<map>
<entry key="作者1" value-ref="author1"/>
<entry key="作者2" value-ref="author2"/>
</map>
</property>
</bean>
<!--注入Author对象-->
<bean id="author1" class="com.haikang.spring.di.pojo.Author">
<property name="name" value="月光"/>
</bean>
<!--注入Author对象-->
<bean id="author2" class="com.haikang.spring.di.pojo.Author">
<property name="name" value="明天"/>
</bean>
</beans>
test
类
@Test
public void test(){
ApplicationContext context =
new ClassPathXmlApplicationContext("collection.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
/**
* 返回值:
* Book{
* arr=[海康, 湛江]
* list=[A_1, B-1]
* sets=[1, 2]
* properties={电话=11001}
* maps={作者1=Author{name='月光'}, 作者2=Author{name='明天'}}
* }
*/
}
4.IOC
两种类型bean
- 普通
bean
- 工厂
bean
(FactoryBean
)
普通Bean
在配置文件中定义bean
类型就是返回类型
如:在bean标签中定义是:Student类型
<bean id="student" class="com.haikang.collection.Student">...</bean>
在获取Bean对象时,返回就是Student类型
Student student = context.getBean("student", Student.class);
普通bean
就是定义的类型和返回类型一致
工厂bean
(FactoryBean
)
实现工厂Bean
步骤:
- 创建类,让该类作为工厂
bean
,需要实现接口FactoryBean
- 实现接口重写方法,在实现的方法中定义返回的
bean
类型
定义类实现工厂FactoryBean接口
public class MyBean implements FactoryBean<User> {
// gerObject就是定义返回Bean类型
@Override
public User getObject() throws Exception {
//简化,其实需要读取和解析xml文件,但是演示做了简化
User user = new User();
return user;
}
@Override
public Class<?> getObjectType() {
return null;
}
}
xml
文件
<?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">
<bean id="myBean" class="com.haikang.factorybean.MyBean"/>
</beans>
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
文件
<?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">
<!--
Bean的作用域:
默认情况下:singleton 属性,表示是单实例的 scope="singleton"
prototype 属性: 表示是多实例
-->
<bean id="person" class="com.haikang.bean.Person" scope="prototype">
<property name="name" value="海康"/>
<property name="age" value="21"/>
</bean>
</beans>
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);
}