Spring
一、导包
- 创建项目 新建一个lib文件夹
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-asfXmDvg-1612182678306)(C:\Users\511003137\AppData\Roaming\Typora\typora-user-images\image-20201215193103967.png)]
二、创建一个接口跟一个实现类
- 建立一个com.hpe.www包 实现CustomerServer接口下的work方法
package com.hpe.www;
public class CustomerServiceImp implements CustomerService{
@Override
public void work() {
// TODO Auto-generated method stub
System.out.println("工作");
}
}
-
src下建立一个applicationcontext.xml文件
在实际开发中如何使用scope配置?
Dao对象: 建议使用单例域
Service对象:建议使用单例域
servlet对象:必须使用多例!!!<?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"> <!-- 创建customerService对象 --> 把这个类注入到bean容器 <bean id="customerService" class="com.hpe.www.CustomerServiceImp" scope="prototype"> //scope 作用域 singleton: 单例域 (默认值) prototype: 多例域 request: 一个请求返回一个对象(web项目) session: 一个会话返回一个对象(web项目) </beans>
-
创建一个访问类
package com.hpe.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.hpe.www.CustomerService; import com.hpe.www.CustomerServiceImp; public class MyTest { public static void main(String []args) { ApplicationContext applicationContext=new FileSystemXmlApplicationContext("./src/applicationcontext.xml"); CustomerService customerService=(CustomerServiceImp) applicationContext.getBean("customerService"); customerService.customerDao.living(); } }
1)ClassPathXmlApplicationContext: 类路径方式加载
2)FileSystemXmlApplicationContext: 文件路径方式加载
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pgrP8Vty-1612182678308)(file:///C:/Users/511003~1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg)]
三、DI 注入
1.1 构造方法注入
package com.hpe.www;
public class CustomerServiceImp implements CustomerService{
private CustomerDao customerDao;
public CustomerServiceImp(CustomerDao customerDao) {
super();
this.customerDao = customerDao;
}
@Override
public void work() {
// TODO Auto-generated method stub
System.out.println("工作");
}
@Override
public CustomerDao getCustomerDao() {
return customerDao;
}
}
<?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">
<!-- 创建customerService对象 -->
<bean id="customerService" class="com.hpe.www.CustomerServiceImp" scope="prototype">
<constructor-arg index="0" ref="customerDao"/><!-- 利用构造方法注入对象 -->
<!-- index:参数索引,从0开始 ref:引用ioc工厂里面的对象 -->
</bean>
<bean id="customerDao" class="com.hpe.www.CustomerDaoImpl"/>
</beans>
1.2 setter方法注入(推荐)
* 通过set方法注入
package com.hpe.www;
public class CustomerServiceImp implements CustomerService{
private CustomerDao customerDao;
public void setCustomerServiceImp(CustomerDao customerDao) {
this.customerDao = customerDao;
}
}
<?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">
<!-- 创建customerService对象 -->
<bean id="customerService" class="com.hpe.www.CustomerServiceImp" scope="prototype">
<property name="customerDao" ref="customerDao"/><!-- 调用setter方法进行对象注入 ,name名称就是setter方法名称-->
</bean>
<bean id="customerDao" class="com.hpe.www.CustomerDaoImpl"/>
</beans>
四、SpringIOC注入不同的数据类型
package com.hpe.www;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.sun.xml.internal.fastinfoset.sax.Properties;
public class CustomerServiceImp {
private int id;
private CustomerDao customerDao;
private String[] name;
private List<String> list;
private Map<String,String> map;
private Set<String> set;
private String wife;
private Properties pro;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public CustomerDao getCustomerDao() {
return customerDao;
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public String[] getName() {
return name;
}
public void setName(String[] name) {
this.name = name;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getPro() {
return pro;
}
public void setPro(Properties pro) {
this.pro = pro;
}
@Override
public String toString() {
return "CustomerServiceImp{" +
"id=" + id +
", customerDao=" + customerDao +
", name=" + Arrays.toString(name) +
", list=" + list +
", map=" + map +
", set=" + set +
", wife='" + wife + '\'' +
", pro=" + pro +
'}';
}
}
<?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="customerDao" class="com.hpe.www.CustomerDao"/>
<!-- 创建customerService对象 -->
<bean id="customerService" class="com.hpe.www.CustomerServiceImp" scope="prototype">
<!-- 基本数据类型+String 使用value属性注入-->
<property name="id" value="32"/>
<property name="wife" value="String注入">
<!-- bean注入 ref-->
<property name="customerDao" ref="customerDao"/>
<!-- 数组注入 ref-->
<property name="name">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>水浒传</value>
<value>西游记</value>
</array>
</property>
<!-- List注入 ref-->
<property name="list">
<value>听歌</value>
<value>看电源</value>
<value>敲代码</value>
<value>打游戏</value>
</property>
<!-- Map注入 ref-->
<property name="map">
<map>
<entry key="身份证" value="12333445"/>
<entry key="银行卡" value="54354345434"/>
</map>
</property>
<!-- set注入 ref-->
<property name="set">
<set>
<value>lou</value>
<value>ddd</value>
<value>ddd</value>
</set>
</property>
<!-- properties注入 ref-->
<property name="pro">
<props>
<prop key="学号">2019</prop>
<prop key="姓名">小米</prop>
<prop key="username">root</prop>
<prop key="password">ww123</prop>
</props>
</property>
</bean>
</beans>
五、注入不同类型
package com.hpe.www;
import java.util.*;
public class CustomerServiceImp {
private double aDouble;
private String name;
private CustomerDao customerDao;
private String []Addresses;
private List<Student> studentsList;
private Map<String,Student> studentMap;
private Properties properties;
public double getaDouble() {
return aDouble;
}
public void setaDouble(double aDouble) {
this.aDouble = aDouble;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CustomerDao getCustomerDao() {
return customerDao;
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public String[] getAddresses() {
return Addresses;
}
public void setAddresses(String[] addresses) {
Addresses = addresses;
}
public List<Student> getStudentsList() {
return studentsList;
}
public void setStudentsList(List<Student> studentsList) {
this.studentsList = studentsList;
}
public Map<String, Student> getStudentMap() {
return studentMap;
}
public void setStudentMap(Map<String, Student> studentMap) {
this.studentMap = studentMap;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "CustomerServiceImp{" +
"aDouble=" + aDouble +
", name='" + name + '\'' +
", customerDao=" + (customerDao.living()) +
", Addresses=" + Arrays.toString(Addresses) +
", studentsList=" + studentsList +
", studentMap=" + studentMap +
", properties=" + properties +
'}';
}
}
<?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="student" class="com.hpe.www.Student" scope="prototype">
<property name="name" value="马超"/>
<property name="id" value="124"/>
<property name="age" value="23"/>
</bean>
<bean id="student1" class="com.hpe.www.Student" scope="prototype">
<property name="name" value="赵云"/>
<property name="id" value="546"/>
<property name="age" value="26"/>
</bean>
<bean id="customerDao" class="com.hpe.www.CustomerDao"/>
<!-- 创建customerService对象 -->
<bean id="customerService" class="com.hpe.www.CustomerServiceImp" scope="prototype">
<!-- 基本数据类型+String 使用value属性注入-->
<property name="aDouble" value="76.55"/>
<property name="name" value="神威将军"/>
<property name="customerDao" ref="customerDao"/>
<property name="addresses">
<array>
<value>深圳</value>
<value>北京</value>
<value>上海</value>
</array>
</property>
<property name="studentsList">
<list>
<ref bean="student"/>
<ref bean="student1"/>
</list>
</property>
<property name="studentMap">
<map>
<entry key="学生" value-ref="student"/>
<entry key="学号" value-ref="student1"/>
</map>
</property>
<property name="properties">
<props>
<prop key="学号">2019</prop>
<prop key="姓名">小米</prop>
<prop key="username">root</prop>
<prop key="password">ww123</prop>
</props>
</property>
</bean>
</beans>
六、Spring的IOC注解开发
1.1 eclipse 导入aop包
-
spring-aop-5.1.8.RELEASE
在applicationcontext中引入context名称空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
1.2 开启扫描注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启SpringIOC的注解功能
让springIOC扫描注解类
base-package: 扫描的类存放的包
-->
<context:component-scan base-package="com.hpe.www"/>
<context:annotation-config/> <!--开启扫描注解 注解驱动的支持-->
</beans>
1.3 在需要创建对象的类使用注解
@Component(value = "customerService") //相当于:<bean id="customerServiceImp" class="com.hpe.www.CustomerServiceImp"/>
@Scope("prototype") //作用域
public class CustomerServiceImp {
@Value("23.54")
private double aDouble;
@Value("神威将军")
private String name;
@Autowired
private CustomerDao customerDao;
private String []Addresses;
@Autowired
private List<Student> studentsList;
@Autowired
private Map<String,Student> studentMap;
}
-
@Component(value = "customerService") value:可以修改bean的id名称
1.4 @Repostory注解
和@Componet一样,为了和三层开发的寓意而设计的注解
这个是为持久层设计的
1.5 @Service注解
和@Componet一样,为了和三层开发的寓意而设计的注解
这个是为业务层设计的
1.6 @Controller注解
和@Componet一样,为了和三层开发的寓意而设计的注解
这个是为表现层设计的
七、 依赖注入的注解
1.1 @Autowired
自动根据类型进行注入 (在spirng的IOC工厂中查询该类型的对象)
1)@Autowired这个注解的底层不是使用setter或者构造方法注入的,使用Java反射技术,给私有成员直接赋值
2)当Spring环境中没有任何一个匹配的类型,直接报错
3)当前Spring环境中出现多个匹配的类型,这时就可以依赖变量名称查询对应的Bean的ID。如果变量名称匹配不上Bean的ID,则报错。
<bean id="c" class="com.hpe.www.Student" autowire="byName" />
<bean id="c" class="com.hpe.www.Student" autowire="byType" />
1.2 @Qualifier
在 @Autowired的基础上,根据bean的id查询
value:需要获取bean的id
@Autowired
@Qualifier(value = "customerDao111")
private CustomerDao customerDao;
1.3 @Resource(推荐)
即可以根据Bean的ID匹配,也可以根据类型自动匹配
name: 需要获取bean的id (推荐使用)
注意:
@Autowired:这个注解来自于Spring框架
@Resource:这个注解来自于Sun公司的标准,这个比@Autowired更标准
1.4 @Value
注入普通类型(8种基本类型+String)
的类型,这时就可以依赖变量名称查询对应的Bean的ID。如果变量名称匹配不上Bean的ID,则报错。
<bean id="c" class="com.hpe.www.Student" autowire="byName" />
<bean id="c" class="com.hpe.www.Student" autowire="byType" />
1.2 @Qualifier
在 @Autowired的基础上,根据bean的id查询
value:需要获取bean的id
@Autowired
@Qualifier(value = "customerDao111")
private CustomerDao customerDao;
1.3 @Resource(推荐)
即可以根据Bean的ID匹配,也可以根据类型自动匹配
name: 需要获取bean的id (推荐使用)
注意:
@Autowired:这个注解来自于Spring框架
@Resource:这个注解来自于Sun公司的标准,这个比@Autowired更标准
1.4 @Value
注入普通类型(8种基本类型+String)