spring的简单用途

一.、什么是spring,它能够做什么?

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
&lt;!-- 讲解ioc的用途 --&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl" id="userBiz"&gt;&lt;/bean&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl2" id="userBiz2"&gt;&lt;/bean&gt;

&lt;bean class="com.spring.action.UserAction" id="userAction"&gt;
	&lt;property name="userBiz" ref="userBiz"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;bean class="com.spring.action.TeaAction" id="teaAction"&gt;
	&lt;property name="userBiz" ref="userBiz2"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UserBizImpl

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl implements UserBiz {

public void doSomething() {
	System.out.println("方法实现一:只为完成功能,不考虑性能");

// System.out.println(“系统升级,性能更优”);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBizImpl2

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl2 implements UserBiz {

public void doSomething() {
	System.out.println("系统升级,性能更优");
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBiz

package com.spring.ioc.biz;

public interface UserBiz {
public void doSomething();
}

  • 1
  • 2
  • 3
  • 4
  • 5

测试类

package com.spring.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.action.ClzAction;
import com.spring.action.StudentAction;
import com.spring.action.TeaAction;
import com.spring.action.UserAction;

public class IocTest {
public static void main(String[] args) {
//通过建模获取spring的上下文,获取到了工程中的所有类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-context.xml”);
UserAction userAction = (UserAction) applicationContext.getBean(“userAction”);
userAction.aaa();

	TeaAction teaAction = (TeaAction) applicationContext.getBean("teaAction");
	teaAction.aaa();
	
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

打印出来的结果为:

方法实现一:只为完成功能,不考虑性能
系统升级,性能更优

 
 
  • 1
  • 2
        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            <div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
							            <div class="markdown_views prism-atom-one-dark">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<h2><a id="spring_0" target="_blank"></a>一.、什么是spring,它能够做什么?</h2>

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
&lt;!-- 讲解ioc的用途 --&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl" id="userBiz"&gt;&lt;/bean&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl2" id="userBiz2"&gt;&lt;/bean&gt;

&lt;bean class="com.spring.action.UserAction" id="userAction"&gt;
	&lt;property name="userBiz" ref="userBiz"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;bean class="com.spring.action.TeaAction" id="teaAction"&gt;
	&lt;property name="userBiz" ref="userBiz2"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UserBizImpl

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl implements UserBiz {

public void doSomething() {
	System.out.println("方法实现一:只为完成功能,不考虑性能");

// System.out.println(“系统升级,性能更优”);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBizImpl2

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl2 implements UserBiz {

public void doSomething() {
	System.out.println("系统升级,性能更优");
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBiz

package com.spring.ioc.biz;

public interface UserBiz {
public void doSomething();
}

  • 1
  • 2
  • 3
  • 4
  • 5

测试类

package com.spring.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.action.ClzAction;
import com.spring.action.StudentAction;
import com.spring.action.TeaAction;
import com.spring.action.UserAction;

public class IocTest {
public static void main(String[] args) {
//通过建模获取spring的上下文,获取到了工程中的所有类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-context.xml”);
UserAction userAction = (UserAction) applicationContext.getBean(“userAction”);
userAction.aaa();

	TeaAction teaAction = (TeaAction) applicationContext.getBean("teaAction");
	teaAction.aaa();
	
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

打印出来的结果为:

方法实现一:只为完成功能,不考虑性能
系统升级,性能更优

 
 
  • 1
  • 2
        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            </div><div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
							            <div class="markdown_views prism-atom-one-dark">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<h2><a id="spring_0" target="_blank"></a>一.、什么是spring,它能够做什么?</h2>

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
&lt;!-- 讲解ioc的用途 --&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl" id="userBiz"&gt;&lt;/bean&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl2" id="userBiz2"&gt;&lt;/bean&gt;

&lt;bean class="com.spring.action.UserAction" id="userAction"&gt;
	&lt;property name="userBiz" ref="userBiz"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;bean class="com.spring.action.TeaAction" id="teaAction"&gt;
	&lt;property name="userBiz" ref="userBiz2"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UserBizImpl

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl implements UserBiz {

public void doSomething() {
	System.out.println("方法实现一:只为完成功能,不考虑性能");

// System.out.println(“系统升级,性能更优”);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBizImpl2

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl2 implements UserBiz {

public void doSomething() {
	System.out.println("系统升级,性能更优");
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBiz

package com.spring.ioc.biz;

public interface UserBiz {
public void doSomething();
}

  • 1
  • 2
  • 3
  • 4
  • 5

测试类

package com.spring.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.action.ClzAction;
import com.spring.action.StudentAction;
import com.spring.action.TeaAction;
import com.spring.action.UserAction;

public class IocTest {
public static void main(String[] args) {
//通过建模获取spring的上下文,获取到了工程中的所有类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-context.xml”);
UserAction userAction = (UserAction) applicationContext.getBean(“userAction”);
userAction.aaa();

	TeaAction teaAction = (TeaAction) applicationContext.getBean("teaAction");
	teaAction.aaa();
	
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

打印出来的结果为:

方法实现一:只为完成功能,不考虑性能
系统升级,性能更优

 
 
  • 1
  • 2
        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            <div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
							            <div class="markdown_views prism-atom-one-dark">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<h2><a id="spring_0" target="_blank"></a>一.、什么是spring,它能够做什么?</h2>

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
&lt;!-- 讲解ioc的用途 --&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl" id="userBiz"&gt;&lt;/bean&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl2" id="userBiz2"&gt;&lt;/bean&gt;

&lt;bean class="com.spring.action.UserAction" id="userAction"&gt;
	&lt;property name="userBiz" ref="userBiz"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;bean class="com.spring.action.TeaAction" id="teaAction"&gt;
	&lt;property name="userBiz" ref="userBiz2"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UserBizImpl

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl implements UserBiz {

public void doSomething() {
	System.out.println("方法实现一:只为完成功能,不考虑性能");

// System.out.println(“系统升级,性能更优”);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBizImpl2

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl2 implements UserBiz {

public void doSomething() {
	System.out.println("系统升级,性能更优");
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBiz

package com.spring.ioc.biz;

public interface UserBiz {
public void doSomething();
}

  • 1
  • 2
  • 3
  • 4
  • 5

测试类

package com.spring.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.action.ClzAction;
import com.spring.action.StudentAction;
import com.spring.action.TeaAction;
import com.spring.action.UserAction;

public class IocTest {
public static void main(String[] args) {
//通过建模获取spring的上下文,获取到了工程中的所有类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-context.xml”);
UserAction userAction = (UserAction) applicationContext.getBean(“userAction”);
userAction.aaa();

	TeaAction teaAction = (TeaAction) applicationContext.getBean("teaAction");
	teaAction.aaa();
	
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

打印出来的结果为:

方法实现一:只为完成功能,不考虑性能
系统升级,性能更优

 
 
  • 1
  • 2
        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            </div></div><div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
							            <div class="markdown_views prism-atom-one-dark">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<h2><a id="spring_0" target="_blank"></a>一.、什么是spring,它能够做什么?</h2>

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
&lt;!-- 讲解ioc的用途 --&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl" id="userBiz"&gt;&lt;/bean&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl2" id="userBiz2"&gt;&lt;/bean&gt;

&lt;bean class="com.spring.action.UserAction" id="userAction"&gt;
	&lt;property name="userBiz" ref="userBiz"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;bean class="com.spring.action.TeaAction" id="teaAction"&gt;
	&lt;property name="userBiz" ref="userBiz2"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UserBizImpl

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl implements UserBiz {

public void doSomething() {
	System.out.println("方法实现一:只为完成功能,不考虑性能");

// System.out.println(“系统升级,性能更优”);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBizImpl2

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl2 implements UserBiz {

public void doSomething() {
	System.out.println("系统升级,性能更优");
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBiz

package com.spring.ioc.biz;

public interface UserBiz {
public void doSomething();
}

  • 1
  • 2
  • 3
  • 4
  • 5

测试类

package com.spring.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.action.ClzAction;
import com.spring.action.StudentAction;
import com.spring.action.TeaAction;
import com.spring.action.UserAction;

public class IocTest {
public static void main(String[] args) {
//通过建模获取spring的上下文,获取到了工程中的所有类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-context.xml”);
UserAction userAction = (UserAction) applicationContext.getBean(“userAction”);
userAction.aaa();

	TeaAction teaAction = (TeaAction) applicationContext.getBean("teaAction");
	teaAction.aaa();
	
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

打印出来的结果为:

方法实现一:只为完成功能,不考虑性能
系统升级,性能更优

 
 
  • 1
  • 2
        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            <div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
							            <div class="markdown_views prism-atom-one-dark">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<h2><a id="spring_0" target="_blank"></a>一.、什么是spring,它能够做什么?</h2>

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
&lt;!-- 讲解ioc的用途 --&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl" id="userBiz"&gt;&lt;/bean&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl2" id="userBiz2"&gt;&lt;/bean&gt;

&lt;bean class="com.spring.action.UserAction" id="userAction"&gt;
	&lt;property name="userBiz" ref="userBiz"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;bean class="com.spring.action.TeaAction" id="teaAction"&gt;
	&lt;property name="userBiz" ref="userBiz2"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UserBizImpl

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl implements UserBiz {

public void doSomething() {
	System.out.println("方法实现一:只为完成功能,不考虑性能");

// System.out.println(“系统升级,性能更优”);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBizImpl2

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl2 implements UserBiz {

public void doSomething() {
	System.out.println("系统升级,性能更优");
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBiz

package com.spring.ioc.biz;

public interface UserBiz {
public void doSomething();
}

  • 1
  • 2
  • 3
  • 4
  • 5

测试类

package com.spring.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.action.ClzAction;
import com.spring.action.StudentAction;
import com.spring.action.TeaAction;
import com.spring.action.UserAction;

public class IocTest {
public static void main(String[] args) {
//通过建模获取spring的上下文,获取到了工程中的所有类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-context.xml”);
UserAction userAction = (UserAction) applicationContext.getBean(“userAction”);
userAction.aaa();

	TeaAction teaAction = (TeaAction) applicationContext.getBean("teaAction");
	teaAction.aaa();
	
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

打印出来的结果为:

方法实现一:只为完成功能,不考虑性能
系统升级,性能更优

 
 
  • 1
  • 2
        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            </div><div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
							            <div class="markdown_views prism-atom-one-dark">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<h2><a id="spring_0" target="_blank"></a>一.、什么是spring,它能够做什么?</h2>

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
&lt;!-- 讲解ioc的用途 --&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl" id="userBiz"&gt;&lt;/bean&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl2" id="userBiz2"&gt;&lt;/bean&gt;

&lt;bean class="com.spring.action.UserAction" id="userAction"&gt;
	&lt;property name="userBiz" ref="userBiz"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;bean class="com.spring.action.TeaAction" id="teaAction"&gt;
	&lt;property name="userBiz" ref="userBiz2"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UserBizImpl

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl implements UserBiz {

public void doSomething() {
	System.out.println("方法实现一:只为完成功能,不考虑性能");

// System.out.println(“系统升级,性能更优”);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBizImpl2

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl2 implements UserBiz {

public void doSomething() {
	System.out.println("系统升级,性能更优");
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBiz

package com.spring.ioc.biz;

public interface UserBiz {
public void doSomething();
}

  • 1
  • 2
  • 3
  • 4
  • 5

测试类

package com.spring.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.action.ClzAction;
import com.spring.action.StudentAction;
import com.spring.action.TeaAction;
import com.spring.action.UserAction;

public class IocTest {
public static void main(String[] args) {
//通过建模获取spring的上下文,获取到了工程中的所有类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-context.xml”);
UserAction userAction = (UserAction) applicationContext.getBean(“userAction”);
userAction.aaa();

	TeaAction teaAction = (TeaAction) applicationContext.getBean("teaAction");
	teaAction.aaa();
	
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

打印出来的结果为:

方法实现一:只为完成功能,不考虑性能
系统升级,性能更优

 
 
  • 1
  • 2
        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            <div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
							            <div class="markdown_views prism-atom-one-dark">
						<!-- flowchart 箭头图标 勿删 -->
						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
						<h2><a id="spring_0" target="_blank"></a>一.、什么是spring,它能够做什么?</h2>

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-autowire="byName"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
&lt;!-- 讲解ioc的用途 --&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl" id="userBiz"&gt;&lt;/bean&gt;
&lt;bean class="com.spring.ioc.biz.impl.UserBizImpl2" id="userBiz2"&gt;&lt;/bean&gt;

&lt;bean class="com.spring.action.UserAction" id="userAction"&gt;
	&lt;property name="userBiz" ref="userBiz"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;bean class="com.spring.action.TeaAction" id="teaAction"&gt;
	&lt;property name="userBiz" ref="userBiz2"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UserBizImpl

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl implements UserBiz {

public void doSomething() {
	System.out.println("方法实现一:只为完成功能,不考虑性能");

// System.out.println(“系统升级,性能更优”);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBizImpl2

package com.spring.ioc.biz.impl;

import com.spring.ioc.biz.UserBiz;

/**

  • 模拟做项目实现某一项功能
  • @author LeiFengJun

*/
public class UserBizImpl2 implements UserBiz {

public void doSomething() {
	System.out.println("系统升级,性能更优");
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

UserBiz

package com.spring.ioc.biz;

public interface UserBiz {
public void doSomething();
}

  • 1
  • 2
  • 3
  • 4
  • 5

测试类

package com.spring.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.action.ClzAction;
import com.spring.action.StudentAction;
import com.spring.action.TeaAction;
import com.spring.action.UserAction;

public class IocTest {
public static void main(String[] args) {
//通过建模获取spring的上下文,获取到了工程中的所有类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-context.xml”);
UserAction userAction = (UserAction) applicationContext.getBean(“userAction”);
userAction.aaa();

	TeaAction teaAction = (TeaAction) applicationContext.getBean("teaAction");
	teaAction.aaa();
	
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

打印出来的结果为:

方法实现一:只为完成功能,不考虑性能
系统升级,性能更优

 
 
  • 1
  • 2
        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
            </div></div></div></div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值