Spring学习笔记-配置文件
Spring入门
1.导入坐标(pom.xml)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.15</version>
</dependency>
2.创建接口类
package org.example.dao;
public interface UserDao {
void save();
}
3.接口实现
package org.example.Impl;
import org.example.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("savr runing...");
}
}
4.创建applicationContext.xml(resouces文件下)配置Bean
<bean id="userDao" class="org.example.Impl.UserDaoImpl"></bean>
5.创建applicationContext对象getBean
public class SpringTest {
@Test
public void test1(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) app.getBean("userDao");
userDao.save();
}
}
Spring配置文件
Bean标签基本配置
基本属性
- id:Bean实例在Spring容器中的唯一标识
- class:Bean的全限定名称
- scope:对象作用范围,取值如下:
-
single:默认值,单例的,当Spring核心被加载时,实例化配置的Bean实例
-
prototype:多例的,当调用getBean()方法时实例化Bean
-
request:WEB项目中,Spring创建一个Bean对象,将对象存到request域中
-
session:WEB项目中,Spring创建一个Bean对象,将对象存到session域中
-
global session:WEB项目中,应用在portlet中,如果没有portlet环境,那么global Session相当于session
Bean生命周期配置
-
init-method:指定类中的初始化方法名称
-
destroy-method:指定类中销毁方法名称
<bean id="userDao" class="org.example.Impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>
public class UserDaoImpl implements UserDao {
public UserDaoImpl() {
System.out.println("对象创建...");
}
@Override
public void save() {
System.out.println("savr runing...");
}
public void init(){
System.out.println("初始化方法....");
}
public void destory(){
System.out.println("销毁方法....");
}
}
Bean实例化的三种方式
无参构造方法实例化
<bean id="userDao" class="org.example.Impl.UserDaoImpl"></bean>
public class UserDaoImpl implements UserDao {
public UserDaoImpl() {}
}
工厂静态方法实例化
<bean id="userDao" class="org.example.factory.StaticFactory" factory-method="getUserDao"></bean>
public class StaticFactory {
public static UserDao getUserDao(){
return new UserDaoImpl();
}
}
工厂实例方法实例化
<bean id="factory" class="org.example.factory.DynameicFactory" ></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean>
public class DynameicFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
对象引用依赖注入方式
构造方法
<bean id="userDao" class="org.example.Impl.UserDaoImpl"></bean>
<bean id="UserService" class="org.example.Impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
public class UserServiceImpl implements UserService {
private UserDao userDao;
//构造方法注入
public UserServiceImpl(UserDao userDao){
this.userDao = userDao;
}
}
set()方法
<bean id="userDao" class="org.example.Impl.UserDaoImpl"></bean>
<bean id="UserService" class="org.example.Impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
<!--name:注入Bean的id,ref:调用方法(setUserDao),去set首字母小写-->
</bean>
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
p命名空间注入(本质set()方法注入,更方便)
<beans xmlns:p="http://www.springframework.org/schema/p">
<!--引入p命名空间-->
<bean id="userDao" class="org.example.Impl.UserDaoImpl"></bean>
<bean id="UserService" class="org.example.Impl.UserServiceImpl" p:userDao-ref="userDao"></bean>
依赖注入数据类型
普通数据类型
<bean id="userDao" class="org.example.Impl.UserDaoImpl">
<property name="username" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
public class UserDaoImpl implements UserDao {
private String username;
private Integer age;
public void setUsername(String username) {
this.username = username;
}
public void setAge(Integer age) {
this.age = age;
}
}
集合数据类型
<bean name="user1" class="org.example.domain.User">
<property name="name" value="Tom"></property>
<property name="addr" value="sh"></property>
</bean>
<bean name="user2" class="org.example.domain.User">
<property name="name" value="Sum"></property>
<property name="addr" value="bj"></property>
</bean>
<bean id="userDao" class="org.example.Impl.UserDaoImpl">
<property name="strList">
<list>
<value>aaa</value>
<value>abb</value>
<value>abc</value>
</list>
</property>
<property name="userMap">
<map>
<entry key="u1" value-ref="user1"></entry>
<entry key="u2" value-ref="user2"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="p1">111</prop>
<prop key="p2">122</prop>
<prop key="p3">123</prop>
</props>
</property>
<property name="age" value="18"></property>
</bean>
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;
}
}
Spring配置文件
<import resource="applicationContext-***.xml"/>