spring的使用
spring配置文件
<?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">
<!--将所有的Java对象都声明在此处 此时spring就可以帮助我们管理bean
就是将所有的对象注册到spring容器-->
<!--注册dao层-->
<bean id="userDao" class="org.lanqiao.dao.impl.UserDaoImpl"/>
<!--注册serviceceng-->
<bean id="userService" class="org.lanqiao.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
配置细节
1 bean的配置
scope
定义bean的创建模式 :单例模式 原型模式
单例模式(Singleton) 在整个生命周期中 bean永远都只有一个对象存在
原型模式(prototype) 每次获取的都是一个新的对象 每次都会new一个新的对象
2 bean的装配
方式一:使用无参构造来创建
<bean id="user" class="org.lanqiao.pojo.User">
</bean>
方式二:使用静态工厂装配bean
public class UserFactory {
public static User getUser(){
return new User();
}
}
<!-- 静态工厂 通过静态工厂 的静态方法 得到我们所需额的目标对象-->
<bean id="user" class="org.lanqiao.pojo.UserFactory" factory-method="getUser">
</bean>
方式三:使用动态工厂装配
public class UserFactory2 {
public User creatUses(){
return new User();
}
}
<!-- 动态工厂-->
<!-- 先将动态工厂类配置为bean-->
<bean id="userFactory" class="org.lanqiao.pojo.UserFactory2"></bean>
<!-- factory-bean为使用工厂的bean的id factory-method为使用的工厂类的方法-->
<bean id="user2" factory-bean="userFactory" factory-method="creatUses">
</bean>
spring属性注入
依赖注入: Dependency Injection。 它是 spring 框架核心 ioc 的具体实现。
我们的程序在编写时, 通过控制反转, 把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。
ioc 解耦只是降低他们的依赖关系,但不会消除。 例如:我们的业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系, 在使用 spring 之后, 就让 spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
方式一:构造函数注入
/**
*/
public class AccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public AccountServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public void saveAccount() {
System.out.println(name+","+age+","+birthday);
}
}
<!-- 使用构造函数的方式,给 service 中的属性传值
要求:
类中需要提供一个对应参数列表的构造函数。
涉及的标签:
constructor-arg
属性:
index:指定参数在构造函数参数列表的索引位置
type:指定参数在构造函数中的数据类型
name:指定参数在构造函数中的名称 用这个找给谁赋值
=======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
value:它能赋的值是基本数据类型和 String 类型
ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
-->
<bean id="accountService" class="com.sp.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
方式二:set方法注入
/** */
public class AccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public void saveAccount() {
System.out.println(name+","+age+","+birthday);
}
}
<!-- 通过配置文件给 bean 中的属性传值:使用 set 方法的方式
涉及的标签:property
属性:
name:找的是类中 set 方法后面的部分
ref:给属性赋值是其他 bean 类型的
value:给属性赋值是基本数据类型和 string 类型的
实际开发中,此种方式用的较多。
-->
<bean id="accountService" class="com.sp.service.impl.AccountServiceImpl">
<property name="name" value="test"></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>
方式三:使用p命名空间注入
此种方式是通过在 xml 中导入 p 名称空间,使用 p:propertyName 来注入数据,它的本质仍然是调用类中的set 方法实现注入功能。
Java 类代码:
/**
* 使用 p 名称空间注入,本质还是调用类中的 set 方法
*/
public class AccountServiceImpl4 implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public void saveAccount() {
System.out.println(name+","+age+","+birthday);
}
}
配置文件需要引入p命名空间,即添加 xmlns:p=“http://www.springframework.org/schema/p”
<!--引入p命名空间-->
<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="accountService"
class="com.sp.service.impl.AccountServiceImpl4"
p:name="test" p:age="21" p:birthday-ref="now"/>
</beans>
方法四:使用c命名空间注入
此种方式是通过在 xml 中导入c 名称空间,使用 c:propertyName 来注入数据,它的本质仍然是调用类中的构造函数
/**
*/
public class AccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public AccountServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public void saveAccount() {
System.out.println(name+","+age+","+birthday);
}
}
配置文件需要引入c命名空间,即添加 xmlns:p=“http://www.springframework.org/schema/c”
<!--引入c命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/c"
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="now" class="java.util.Date"></bean>
<bean id="accountService"
class="com.sp.service.impl.AccountServiceImpl4"
c:name="test" c:age="21" c:birthday-ref="now"/>
</beans>
集合属性的注入
<!--======= list属性-->
<property name="list">
<list>
<value>aaa</value>
<value>bbb</value>
<value>accc</value>
<value>ddd</value>
</list>
</property>
<!--======== 数组属性-->
<property name="arr">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</array>
</property>
<!--======== map属性注入 方式一-->
<property name="map">-->
<props>-->
<prop key="A">aaaa</prop>
<prop key="B">bbb</prop>
<prop key="C">ccc</prop>
<prop key="D">ddd</prop>
</props>-->
</property>
<!--======== map属性注入 方式二-->
<property name="map">
<map>
<entry key="aaa" value="AAAA"></entry>
<entry key="bbbb" value="BBBB"></entry>
<entry key="ccc" value="CCCC"></entry>
</map>
</property>
</bean>