Spring默认的类型时JDK动态代理
对实现类对象做增强得到的增强类与实现类是兄弟关系,所以不能用实现类接收增强类对象,只能用接口接收
如果直接使用自然会报错
配置文件
Bean标签基本配置
<!--
bean:配置一个bean对象,将对象交给IOC容器管理
属性:
id :bean标签的唯一标识,不能重复
class:设置bean对象所对应的类型(实现类)
-->
<bean id="随机命名" class="实现类路径.实现类名" 各种标签配置></bean>
用来配置对象交由Spring来创建
默认情况下它调用的是类中的 无参构造函数 ,如果没有无参构造函数则不能创建成功
基本属性:
id : Bean实例在Spring容器中的唯一标识
class : Bean的全限定名称
标签范围配置
scope :指对象的作用范围,无定义则默认singleton,取值如图:
Bean实例化三种方式
①无参构造方法实例化(常用需掌握)
实现类UserDao.class
import com.dao.UserDao;
public class UserDaoImpl implements UserDao {
/**
* 保存用户信息
*/
@Override
public void save() {
}
}
applicationContext.xml文件下
<bean id="userDao" class="com.dao.impl.UserDaoImpl"></bean>
②工厂静态方法实例化
package com.factory;
import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
public class StaticFactory {
public static UserDao getUserDao(){
return new UserDaoImpl();
}
}
applicationContext.xml文件下加入:
<bean id="userDao" class="com.factory.StaticFactory" factory-method="getUserDao"></bean>
③工厂实例方法实例化
package com.factory;
import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
public class DynamicFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
applicationContext.xml文件下加入:
<bean id="factory" class="com.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"/>
Bean依赖注入
一、方法注入:
①set方法注入
package com.library.service.impl;
import com.library.dao.UserDao;
import com.library.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
applicationContext.xml文件下加入:
<bean id="UserService" class="com.library.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
代码解释:
②P命名空间注入
p命名空间注入本质也是set方法注入,但比起上述的set方法注入更加方便,主要体现在配置文件中,如下:首先,需要引入P命名空间:
package com.library.service.impl;
import com.library.dao.UserDao;
import com.library.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.library.dao.impl.UserDaoImpl"></bean>
<bean id="UserService" class="com.library.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
</beans>
代码解释:
③构造方法注入
package com.library.service.impl;
import com.library.dao.UserDao;
import com.library.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
applicationContext.xml文件下加入:
<bean id="UserService" class="com.library.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
代码解释
二、值注入
整体一样和上面有三种方法,且使用方法步骤一样,只有引用的地方用的是value而不是ref
①set方法注入
package com.library.dao.impl;
import com.library.dao.UserDao;
public class UserDaoImpl implements UserDao {
private String username;
private int age;
public void setUsername(String username) {
this.username = username;
}
public void setAge(int age) {
this.age = age;
}
/**
* 保存用户信息
*/
@Override
public void save() {
System.out.println(username+"=="+age);
}
}
applicationContext.xml文件下加入:
<bean id="userDao" class="com.library.dao.impl.UserDaoImpl">
<property name="username" value="张三"/>
<property name="age" value="18" />
</bean>
代码解释
三、集合注入
①set方法注入
package com.library.dao.impl;
import com.library.dao.UserDao;
import com.library.demo.*;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class UserDaoImpl implements UserDao {
private List<String> strList;
private Map<String, User>userMap;
private Properties properties;
public void setStrList(List<String> strList) {
this.strList = strList;
}
public void setUserMap(Map<String, User> userMap) {
this.userMap = userMap;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
/**
* 保存用户信息
*/
@Override
public void save() {
System.out.println(strList);
System.out.println(userMap);
System.out.println(properties);
}
}
applicationContext.xml文件下加入:
<property name="strList" >
<list>
<value>数据1</value>
<value>数据2</value>
<value>数据3</value>
</list>
</property>
</bean>
<bean id="userDao" class="com.library.dao.impl.UserDaoImpl">
<property name="userMap" >
<map>
<entry key="u1" value-ref="user1"></entry>
</map>
</property>
</bean>
<bean id="user1" class="com.library.demo.User">
<property name="name" value="张三"/>
<property name="id" value="123"/>
</bean>
<bean id="user1" class="com.library.demo.User">
<property name="name" value="李四"/>
<property name="id" value="456"/>
</bean>
<bean id="userDao" class="com.library.dao.impl.UserDaoImpl">
<property name="properties" >
<props>
<prop key="p1">ppp1</prop>
<prop key="p2">ppp2</prop>
<prop key="p3">ppp3</prop>
</props>
</property>
</bean>
代码解释
引入其他配置文件
该作用是:分模块开发,避免把所有配置集中在一个文件中,不宜于维护
<import resource="其他配置文件名.xml"></import>
Spring相关API
ApplicationContext的实现类