因为Spring 功能太强大,所有不可能所有功能的jar包都导进项目中,
因此我们用到什么项目就导什么jar包,比如下面的分析IOC控制反转的代码就
只需要导入以下jar包:
导入jar包后记得add buildpath一下~
随便写两个domain类:
package cn.hncu.ioc.domain;
public class User {
public User() {
System.out.println("构造一个User对象....");
}
}
package cn.hncu.ioc.domain;
public class Customer {
private String name;
private Integer age;
private User user;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the age
*/
public Integer getAge() {
return age;
}
/**
* @return the user
*/
public User getUser() {
return user;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param age the age to set
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* @param user the user to set
*/
public void setUser(User user) {
this.user = user;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Customer [name=" + name + ", age=" + age + ", user=" + user
+ "]";
}
}
接下来是就是写applicationContext.xml配置文件:
注意要导入schema约束,
知识点1:spring创建的bean默认是单例, 但可以通过添加scope属性设置成request,session内的单例
或者设成原型(每次都是一个新的)
知识点2:在定义bean的时候,可以进行数据封装
知识点3:Spring配置文件默认位置和文件名,classpath: applicationContext.xml
Spring容器一创建,就会把xml中定义的所有bean创建并初始化好(对一般的单例bean),放在容器中。
若是原型范畴的bean是每次获取时再创建并初始化
<?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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- spring创建的bean默认是单例, 但可以通过添加scope属性设置成request,session内的单例 或者设成原型(每次都是一个新的) -->
<!-- <bean id="u1" class="cn.hncu.ioc.domain.User" scope="prototype"></bean> -->
<bean id="u1" class="cn.hncu.ioc.domain.User" ></bean>
<!-- 在定义bean的时候,可以进行数据封装 -->
<bean id="cus1" class="cn.hncu.ioc.domain.Customer">
<property name="name" value="Jack"></property>
<property name="age" value="22"></property>
<property name="user" ref="u1"></property>
</bean>
</beans>
接下来就写一个测试类,让我们见识一下Spring的强大吧:
package cn.hncu.ioc.demo1;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.hncu.ioc.domain.User;
public class springDemo {
@Test
public void demo1() {
// Spring配置文件默认位置和文件名,classpath: applicationContext.xml
// Spring容器一创建,就会把xml中定义的所有bean创建并初始化好(对于
// 一般的单例bean),放在容器中。若是原型范畴的bean是每次获取时再创建并初始化
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
User user = ctx.getBean(User.class);
System.out.println(user);
User u2=(User) ctx.getBean("u1");
System.out.println(u2);
new MyThread(ctx).start();
}
}
class MyThread extends Thread{
private ApplicationContext ctx;
public MyThread(ApplicationContext ctx) {
this.ctx = ctx;
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
@Override
public void run() {
User u3 = ctx.getBean(User.class);
System.out.println("u3:"+u3);
}
}
若按照下面这句,则结果如下图:
<bean id="u1" class="cn.hncu.ioc.domain.User" scope="prototype"></bean>
这就是IOC控制反转,也就是我们说的容器最简单的演示。
接下来让我们理一下流程和理论化:
IoC(Inversion of Control)中文译为控制反转也可以叫做DI(Dependency Injection,依赖注入)。
控制反转模式的基本概念是:不直接创建对象,但是在xml配置文件中描述创建它们的方式。
在工程中使用该Bean时由Spring容器创建Bean的实例。
在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。
1、Spring注入(又称依赖注入DI):其目的是为其中bean的属性赋值
2: 在配置文件中注入属性的初始值
上面的例子只是为了演示容器的效果,接下来模拟一下用户登录,利用Spring ioc控制反转:
dao层实现类:
package cn.hncu.ioc.demo2.dao;
public class LoginDaoMysql implements LoginDAO {
@Override
public boolean login(String name, String pwd) {
System.out.println("到MySQL数据库中进行登录校验...."+name+","+pwd);
return false;
}
}
public class LoginDaoOracle implements LoginDAO{
@Override
public boolean login(String name, String pwd) {
System.out.println("到Oracle数据库中进行登录校验...."+name+","+pwd);
return false;
}
}
dao层接口:
public interface LoginDAO {
public boolean login(String name,String pwd);
}
Action:
知识点:注入service ---注意,service的setter-getter必须要写
public class LoginAction {
// 注入service ---注意,service的setter-getter必须要写
private ILoginService service = null;
private String name;
private String pwd;
/**
* @return the service
*/
public ILoginService getService() {
return service;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the pwd
*/
public String getPwd() {
return pwd;
}
/**
* @param service the service to set
*/
public void setService(ILoginService service) {
this.service = service;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param pwd the pwd to set
*/
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String execute(){
boolean boo = service.login(name,pwd);
System.out.println(boo);
return "success";
}
}
Service:
public interface ILoginService {
public boolean login(String name,String pwd);
}
public class LoginServiceImpl implements ILoginService{
//注入Dao
private LoginDAO dao=null;
/**
* @return the dao
*/
public LoginDAO getDao() {
return dao;
}
/**
* @param dao the dao to set
*/
public void setDao(LoginDAO dao) {
this.dao = dao;
}
@Override
public boolean login(String name, String pwd) {
return dao.login(name, pwd);
}
}
配置文件:
知识点:ref即引用,这样通过setter-getter就能注入
<?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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- <bean id="u1" class="cn.hncu.ioc.domain.User" scope="prototype"></bean> -->
<bean id="action1" class="cn.hncu.ioc.demo2.action.LoginAction" >
<property name="name" value="Tom"></property>
<property name="pwd" value="666"></property>
<property name="service" ref="ser"></property>
</bean>
<bean id="ser" class="cn.hncu.ioc.demo2.service.LoginServiceImpl">
<property name="dao" ref="dao2"></property>
</bean>
<bean id="dao1" class="cn.hncu.ioc.demo2.dao.LoginDaoMysql"></bean>
<bean id="dao2" class="cn.hncu.ioc.demo2.dao.LoginDaoOracle"></bean>
</beans>
Main:
package cn.hncu.ioc.demo2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.hncu.ioc.demo2.action.LoginAction;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("cn/hncu/ioc/demo2/demo2.xml");
LoginAction action=ctx.getBean(LoginAction.class);
action.execute();
}
}
接下来是第三版本,除了单值,bean以外,多值也一样可以:
Person类:
package cn.hncu.ioc.demo3;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cn.hncu.ioc.domain.Customer;
public class Person {
// 单值
private String name;
private Integer age;
private Customer customer;
// 多值(集合、数组)
private List<String> names;
private Map<String, Object> map;
private Set<String> set;
private Object[] objs;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the age
*/
public Integer getAge() {
return age;
}
/**
* @return the customer
*/
public Customer getCustomer() {
return customer;
}
/**
* @return the names
*/
public List<String> getNames() {
return names;
}
/**
* @return the map
*/
public Map<String, Object> getMap() {
return map;
}
/**
* @return the set
*/
public Set<String> getSet() {
return set;
}
/**
* @return the objs
*/
public Object[] getObjs() {
return objs;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param age the age to set
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* @param customer the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}
/**
* @param names the names to set
*/
public void setNames(List<String> names) {
this.names = names;
}
/**
* @param map the map to set
*/
public void setMap(Map<String, Object> map) {
this.map = map;
}
/**
* @param set the set to set
*/
public void setSet(Set<String> set) {
this.set = set;
}
/**
* @param objs the objs to set
*/
public void setObjs(Object[] objs) {
this.objs = objs;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", customer="
+ customer + ", names=" + names + ", map=" + map + ", set="
+ set + ", objs=" + Arrays.toString(objs) + "]";
}
}
配置文件:
<?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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="u1" class="cn.hncu.ioc.domain.User"></bean>
<bean id="c1" class="cn.hncu.ioc.domain.Customer">
<property name="name" value="Jack"></property>
<property name="age" value="22"></property>
<property name="user" ref="u1"></property>
</bean>
<bean class="cn.hncu.ioc.demo3.Person">
<!-- 单值注入 -->
<property name="name" value="张三"></property>
<property name="age" value="24" />
<property name="customer" ref="c1"></property>
<!-- 多值注入,用一个专有的集合标签去封装value -->
<property name="names">
<list>
<value>AAA</value>
<value>BBB</value>
<value>三三三</value>
<value>四轮</value>
</list>
</property>
<property name="map">
<map>
<entry key="name" value="国庆"></entry>
<entry key="cus" value-ref="c1"></entry>
</map>
</property>
<property name="set">
<set>
<value>aaa111</value>
<value>100</value>
<value>bb222</value>
</set>
</property>
<property name="objs">
<array>
<value>字符串:Hello World!</value>
<ref bean="t1"/>
<bean class="cn.hncu.ioc.demo3.Cat">
<property name="name" value="abc123"></property>
</bean>
<list>
<value>1111</value>
<value>2222</value>
</list>
</array>
</property>
</bean>
<import resource="dd.xml"/>
</beans>