Java後端的學習之Spring基礎java
若是要學習spring
,那麼什麼是框架,spring
又是什麼呢?學習spring
中的ioc
和bean
,以及aop
,IOC
,Bean
,AOP
,(配置,註解,api
)-springFramework
.web
各類學習的知識點:spring
spring expression language
spring integration
spring web flow
spring security
spring data
spring batch
複製代碼
spring
網站: http://spring.io/
數據庫
http://spring.io/projects/spring-framework
express
spring
是一種開源框架,是爲了解決企業應用開發的複雜性問題而建立的,如今的發展已經不止於用於企業應用了.編程
spring
是一種輕量級的控制反轉(IoC)和面向切面(AOP)的容器框架.windows
一句名言:spring
帶來了複雜的javaee
開發的春天.後端
jdbc orm
oxm jms
transactions
websocket servlet
web portlet
aop aspects instrumentation messaging
beans core context spel
複製代碼
springmvc+spring+hibernate/ibatis
->企業應用api
什麼是框架:數組
框架就是別人制定好的一套規則和規範,你們在這個規範或者規則下進行工做,能夠說,別人蓋好了樓,讓咱們住.
軟件框架是一種半成品,具備特定的處理流程和控制邏輯,成熟的,能夠不斷升級和改進的軟件.
使用框架重用度高,開發效率和質量的提升,容易上手,快速解決問題.
spring ioc
容器
接口,是用於溝通的中介物的,具備抽象化,java
中的接口,就是聲明瞭哪些方法是對外公開的.
面向接口編程,是用於隱藏具體實現的組件.
案例:
// 聲明一個接口
public interface DemoInterface{
String hello(String text);
// 一個hello方法,接收一個字符串型的參數,返回一個`String`類型.
}
// 實現
public class OneInterface implements DemoInterface{
@Override
public String hello(String text){
return "你好啊: " + text;
}
}
// 測試類
public class Main{
public static void main(String[] args){
DemoInterface demo = new OneInterface();
System.out.println(demo.hello("dashucoding");
}
}
複製代碼
什麼是IOC
,IOC
是控制反轉,那麼什麼控制反轉,控制權的轉移,應用程序不負責依賴對象的建立和維護,而是由外部容器負責建立和維護.
<?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="oneInterface" class="com.ioc.interfaces.OneInterfaceImpl"></bean>
</beans>
複製代碼
spring.xml
測試:
import org.junit.Test;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestOneInterface extends UnitTestBase {
public TestOneInterface(){
super("spring.xml");
}
@Test
public void testHello(){
OneInterface oneInterface = super.getBean("oneInterface");
System.out.println(oneInterface.hello("dashucoding"));
}
}
複製代碼
單元測試
下載一個包junit-*.jar
導入項目中,而後建立一個UnitTestBase
類,用於對spring
進行配置文件的加載和銷燬,全部的單元測試都是繼承UnitTestBase
的,而後經過它的getBean
方法獲取想要的對象,子類要加註解@RunWith(BlockJUnit4ClassRunner.class)
,單元測試方法加註解@Test
.
public ClassPathXmlApplicationContext context;
public String springXmlpath;
public UnitTestBase(){}
public UnitTestBase(String springXmlpath){
this.springXmlpath = springXmlpath;
}
@Before
public void before(){
if(StringUtils.isEmpty(springXmlpath)){
springXmlpath = "classpath*:spring-*.xml";
}
try{
context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
}catch(BeansException e){
e.printStackTrace();
}
}
@After
public void after(){
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId){
return (T)context.getBean(beanId);
}
protected <T extends Object> T getBean(Class<T> clazz){
return context.getBean(clazz);
}
}
複製代碼
bean
容器:
org.springframework.beans
和org.springframework.context
BeanFactory
提供配置結構和基本功能,加載並初始化Bean
,ApplicationContext
保存了Bean
對象.
// 文件
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:/appcontext.xml");
// Classpath
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
// Web應用
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
複製代碼
spring
注入:啓動spring
加載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">
<bean id="iService" class="com.service.iServiceImpl">
<property name="iDAO" ref="DAO"/>
</bean>
<bean id="DAO" class="com.iDAOImpl"></bean>
</beans>
複製代碼
// 構造注入
<?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="iService" class="com.service.iServiceImpl">
<constructor-arg name="DAO" ref="DAO"/>
<property name="injectionDAO" ref="injectionDAO"></property>
</bean>
<bean id="DAO" class="com.iDAOImpl"></bean>
</beans>
複製代碼
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">
<bean id="injectionService" class="com.injection.service.InjectionServiceImpl"></bean>
<bean id="injectionDAO" class="com.ijection.dao.InjectionDAOImpl"></bean>
</beans>
複製代碼
// 接口-業務邏輯
public interface InjectionService {
public void save(String arg);
}
複製代碼
// 實現類-處理業務邏輯
public class InjectionServiceImpl implements InjecionService {
private InjectionDAO injectionDAO;
public InjectionServiceImpl(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
public void setInjectionDAO(InjectiionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
public void save(String arg) {
System.out.println("接收" + arg);
arg = arg + ":" + this.hashCode();
injectionDAO.save(arg);
}
}
複製代碼
// 接口-數據庫-調用DAO
public interface InjectionDAO {
// 聲明一個方法
public void save(String arg);
}
複製代碼
// 實現類
public class InjectionDAOImpl implements InjectionDAO {
// 實現接口中的方法
public void save(String arg) {
System.out.println("保存數據" + arg);
}
}
複製代碼
// 測試
import org.junit.Test;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase {
public TestInjection(){
super("classpath:spring-injection.xml");
}
@Test
public void testSetter(){
InjectionService service = super.getBean("injectionService");
service.save("保存的數據");
}
@Test
public void testCons() {
InjectionService service = super.getBean("injectionService");
service.save("保存的數據");
}
}
複製代碼
bean
的配置:
id:id是整個ioc容器中,bean的標識
class:具體要實例化的類
scope:做用域
constructor arguments:構造器的參數
properties:屬性
autowiring mode:自動裝配的模式
lazy-initialization mode:懶加載模式
Initialization/destruction method:初始化和銷燬的方法
複製代碼
做用域
singleton:單例
prototype:每次請求都建立新的實例
request:每次http請求都建立一個實例有且當前有效
session:同上
複製代碼
spring bean
配置之Aware
接口:spring
中提供了以Aware
結尾的接口,爲spring
的擴展提供了方便.
bean
的自動裝配autowiring
no是指不作任何操做
byname是根據本身的屬性名自動裝配
byType是指與指定屬性類型相同的bean進行自動裝配,若是有過個類型存在的bean,那麼就會拋出異常,不能使用byType方式進行自動裝配,若是沒有找到,就不什麼事都不會發生
Constructor是與byType相似,它是用於構造器參數的,若是沒有找到與構造器參數類型一致的bean就會拋出異常
複製代碼
spring bean
配置的resource
resources:
urlresource是url的資源
classpathresource是獲取類路徑下的資源
filesystemresource是獲取文件系統的資源
servletcontextresource是servletcontext封裝的資源
inputstreamresource是針對輸入流封裝的資源
bytearrayresource是針對字節數組封裝的資源
複製代碼
public interface ResourceLoader{
Resource getResource(String location);
}
複製代碼
ResourceLoader
classpath: Loaded from the classpath;
file: Loaded as a URL, from the filesystem;
http: Loaded as a URL;
複製代碼
案例:
public class MResource implements ApplicationContextAware{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void resource(){
Resource resource = applicationContext.getResource("classpath:config.txt");
System.out.println(resource.getFilename());
}
}
複製代碼
// 單元測試類
import com.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestResource extends UnitTestBase {
public TestResource() {
super("classpath:spring-resource.xml");
}
@Test
public void testResource() {
MResource resource = super.getBean("mResource");
try{
resource.resource();
}catch(IOException e){
e.printStackTrace();
}
}
}
複製代碼
<?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="moocResource" class="com.resource.MResource"></bean>
</beans>
複製代碼
bean
的定義與學習:
<context:annotation-config/>
@Component,@Repository,@Service,@Controller
@Required,@Autowired,@Qualifier,@Resource
複製代碼
@Configuration,@Bean,@Import,@DependsOn
@Component,@Repository,@Service,@Controller
複製代碼
@Repository
用於註解DAO
類爲持久層@Service
用於註解Service
類爲服務層@Controller
用於Controller
類爲控制層元註解Meta-annotations
是spring
提供的註解能夠做爲字節的代碼叫元數據註解,處理value()
,元註解能夠有其餘的屬性.
spring
能夠自動檢測和註冊bean
@Service
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public SimpleMovieLister(MovieFinder movieFinder){
this.movieFinder = movieFinder;
}
}
複製代碼
@Repository
public class JpaMovieFinder implements MovieFinder {
}
複製代碼
類的自動檢測以及
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">
<context:component-scan base-package="org.example"/>
</beans>
複製代碼
類被自動發現並註冊
bean
的條件:
用@Component,@Repository,@Service,@Controller註解或者使用@Component的自定義註解
複製代碼
@Required
用於bean
屬性的setter
方法 @Autowired
註解
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
用於構造器或成員變量
@Autowired
private MovieCatalog movieCatalog;
private CustomePreferenceDap customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
複製代碼
@Autowired
註解
使用這個註解,若是找不到bean
將會致使拋出異常,可使用下面代碼避免,每一個類只能有一個構造器被標記爲required=true
.
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired(required=false)
public void setMovieFinder(MovieFinder movieFinder){
this.movieFinder = movieFinder;
}
}
複製代碼
spring
是一個開源框架,spring是用j2ee開發的mvc框架,spring boot呢就是一個能整合組件的快速開發框架,由於使用maven管理,因此很便利。至於spring cloud,就是微服務框架了。
spring
是一個輕量級的Java
開發框架,是爲了解決企業應用開發的複雜性而建立的框架,框架具備分層架構的優點.
spring
這種框架是簡單性的,可測試性的和鬆耦合的,spring
框架,咱們主要是學習控制反轉IOC
和麪向切面AOP
.
// 知識點
spring ioc
spring aop
spring orm
spring mvc
spring webservice
spring transactions
spring jms
spring data
spring cache
spring boot
spring security
spring schedule
複製代碼
spring ioc
爲控制反轉,控制反向,控制倒置,
Spring
容器是 Spring
框架的核心。spring
容器實現了相互依賴對象的建立,協調工做,對象只要關係業務邏輯自己,IOC
最重要的是完成了對象的建立和依賴的管理注入等,控制反轉就是將代碼裏面須要實現的對象建立,依賴的代碼,反轉給了容器,這就須要建立一個容器,用來讓容器知道建立對象與對象的關係.(告訴spring你是個什麼東西,你須要什麼東西)
xml,properties等用來描述對象與對象間的關係
classpath,filesystem,servletContext等用來描述對象關係的文件放在哪裏了.
複製代碼
控制反轉就是將對象之間的依賴關係交給了容器管理,原本是由應用程序管理的對象之間的依賴的關係.
spring ioc
體系結構
BeanFactory
BeanDefinition
複製代碼
spring ioc
是spring
的核心之一,也是spring體系的基礎,在
spring中主要用戶管理容器中的
bean.
spring的
IOC容器主要使用
DI方式實現的.
BeanFactory是典型的工廠模式,
ioc容器爲開發者管理對象間的依賴關係提供了不少便利.在使用對象時,要
new object()來完成合做.
ioc:
spring容器是來實現這些相互依賴對象的建立和協調工做的.(由
spring`來複雜控制對象的生命週期和對象間的)
全部的類的建立和銷燬都是由spring
來控制,再也不是由引用它的對象了,控制對象的生命週期在spring
.全部對象都被spring
控制.
ioc
容器的接口(本身設計和麪對每一個環節):
BeanFactory
工廠模式
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String name) throws BeansException;
Object getBean(String name, Class requiredType) throws BeansException;
boolean containsBean(String name);
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
Class getType(String name) throws NoSuchBeanDefinitionException;
String[] getAliases(String name);
}
複製代碼
BeanFactory
三個子類:ListableBeanFactory
,HierarchicalBeanFactory
和AutowireCapableBeanFactory
,實現類是DefaultListableBeanFactory
.
控制反轉就是全部的對象都被spring
控制.ioc
動態的向某個對象提供它所須要的對象.經過DI
依賴注入來實現的.如何實現依賴注入ID
,在Java
中有一特性爲反射,它能夠在程序運行的時候進行動態的生成對象和執行對象的方法,改變對象的屬性.
public static void main(String[] args){
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
Animal animal = (Animal)context.getBean("animal");
animal.say();
}
複製代碼
// applicationContext.xml
<bean id="animal" class="com.test.Cat">
<property name="name" value="dashu"/>
</bean>
複製代碼
public class Cat implements Animal {
private String name;
public void say(){
System.out.println("dashu");
}
public void setName(String name){
this.name = name;
}
}
複製代碼
public interface Animal {
public void say();
}
複製代碼
// bean
private String id;
private String type;
private Map<String,Object> properties=new HashMap<String, Object>();
複製代碼
<bean id="test" class="Test">
<property name="testMap">
</property>
</bean>
複製代碼
public static Object newInstance(String className) {
Class<?> cls = null;
Object obj = null;
try {
cls = Class.forName(className);
obj = cls.newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return obj;
}
複製代碼
核心是控制反轉(
IOC
)和麪向切面(AOP
),spring
是一個分層的JavaSE/EE
的輕量級開源框架.
web
:
struts,spring-mvc
複製代碼
service
:
spring
複製代碼
dao
:
mybatis,hibernate,jdbcTemplate,springdata
複製代碼
spring
體系結構ioc
// 接口
public interface UserService {
public void addUser();
}
// 實現類
public class UserServiceImpl implements UserService {
@Override
public void addUser(){
System.out.println("dashucoding");
}
}
複製代碼
配置文件:
<?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="userServiceId" class="com.dashucoding.UserServiceImpl"></bean>
</beans>
複製代碼
測試:
@Test
public void demo(){
String xmlPath = "com/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
}
複製代碼
依賴注入:
class DemoServiceImpl{
private daDao daDao;
}
複製代碼
建立
service
實例,建立dao
實例,將dao
設置給service
.
接口和實現類:
public interface BookDao {
public void addBook();
}
public class BookDaoImpl implements BookDao {
@Override
public void addBook() {
System.out.println("dashucoding");
}
}
複製代碼
public interface BookService {
public abstract void addBook();
}
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public void addBook(){
this.bookDao.addBook();
}
}
複製代碼
配置文件:
<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="bookServiceId" class="com.BookServiceImpl">
<property name="bookDao" ref="bookDaoId"></property>
</bean>
<bean id="bookDaoId" class="com.BookDaoImpl"></bean>
</beans>
複製代碼
測試:
@Test
public void demo(){
String xmlPath = "com/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
BookService bookService = (BookService) applicationContext.getBean("bookServiceId");
bookService.addBook();
}
複製代碼
IDE
創建Spring
項目
File—>new—>project—>Spring
複製代碼
spring
// Server.java
public class Server {
privete String name;
public void setName(String name){
this.name = name;
}
public void putName(){
System.out.println(name);
}
}
複製代碼
// Main.java
public class Main{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Server hello = (Server)context.getBean("example_one");
hello.putName();
}
}
複製代碼
spring-config.xml
:
<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="example_one" class="Server">
<property name="name" value="達叔小生"></property>
</bean>
</beans>
複製代碼
使用Maven
來聲明Spring
庫.Maven
是一個項目管理的工具,maven
提供了開發人員構建一個完整的生命週期框架.Maven
的安裝和配置,須要的是JDK 1.8
,Maven
,Windows
,配置jdk
,JAVA_HOME
變量添加到windows
環境變量.下載Apache Maven
,添加 M2_HOME
和 MAVEN_HOME
,添加到環境變量PATH
,值爲%M2_HOME%\bin
.執行mvn –version
命令來顯示結果.
Maven
啓用代理進行訪問,找到文件路基,找到/conf/settings.xml
,填寫代理,要阿里的哦.
Maven
中央存儲庫地址: https://search.maven.org/
// xml
<dependency>
<groupId>org.jvnet.localizer</groupId>
<artifactId>localizer</artifactId>
<version>1.8</version>
</dependency>
複製代碼
// pom.xml
<repositories>
<repository>
<id>java.net</id>
<url>https://maven.java.net/content/repositories/public/</url>
</repository>
</repositories>
複製代碼
Maven
添加遠程倉庫:
// pom.xml
<project ...>
<repositories>
<repository>
<id>java.net</id>
<url>https://maven.java.net/content/repositories/public/</url>
</repository>
</repositories>
</project>
<project ...>
<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
</project>
複製代碼
Maven
依賴機制,使用Maven
建立Java
項目.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
複製代碼
Maven
打包:
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dashucoding</groupId>
<artifactId>NumberGenerator</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
複製代碼
spring
框架:
public interface HelloWorld{
public void sayHello();
}
public class SpringHelloWorld implements HelloWorld {
public void sayHello(){
System.out.println("Spring Hello");
}
}
public class StrutsHelloWorld implements HelloWorld {
public void sayHello(){
System.out.println("Struts Hello");
}
}
public class HelloWorldServie {
private HelloWorld helloWorld;
public HelloWorldService(){
this.helloWorld = new StrutsHelloWorld();
}
}
複製代碼
控制反轉:
public class HelloWorldService{
private HelloWorld helloWorld;
public HelloWorldService(){
}
public void setHelloWorld(HelloWorld helloWorld){
this.helloWorld = helloWorld;
}
public HelloWorld getHelloWorld(){
return this.helloWorld;
}
}
複製代碼
ioc
建立了HelloWorldService
對象.
spring->HelloProgram.java
helloworld->
HelloWorld.java
HelloWorldService.java
impl實現類->
SpringHelloWorld.java
StrutsHelloWorld.java
resources->beans.xml
// 總結
一個spring:HelloProgram.java
接口:
實現類:
資源:beans.xml
複製代碼
// HelloWorld.java
public interface HelloWorld {
public void sayHello();
}
// public class HelloWorldService {
private HelloWorld helloWorld;
public HelloWorldService(){
}
public void setHelloWorld(HelloWorld helloWorld){
this.helloWorld = helloWorld;
}
public HelloWorld getHelloWorld(){
return this.helloWorld;
}
}
複製代碼
// SpringHelloWorld.java
public class SpringHelloWorld implements HelloWorld {
@Override
public void sayHello() {
System.out.println("Spring Hello!");
}
}
// StrutsHelloWorld.java
public class StrutsHelloWorld implements HelloWorld {
@Override
public void sayHello() {
System.out.println("Struts Hello!");
}
}
複製代碼
// HelloProgram.java
public class HelloProgram {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
HelloWorldService service =
(HelloWorldService) context.getBean("helloWorldService");
HelloWorld hw= service.getHelloWorld();
hw.sayHello();
}
}
複製代碼
// beans.xml
<beansxmlns="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="springHelloWorld"
class="com.spring.helloworld.impl.SpringHelloWorld"></bean>
<bean id="strutsHelloWorld"
class="com.spring.helloworld.impl.StrutsHelloWorld"></bean>
<bean id="helloWorldService"
class="com.spring.helloworld.HelloWorldService">
<property name="helloWorld" ref="springHelloWorld"/>
</bean>
</beans>
複製代碼
<propertyname="helloWorld"ref="strutsHelloWorld"/>
複製代碼
ioc
建立beans
實現類springHelloWorld
,建立一個helloWorldService
類,beans.xml
實現參數導入:
// helloWorldService
// springHelloWorld
// Hello Program.java
ApplicationContext context = new ClassPathXmlApplicationContxt("beans.xml");
HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");
HelloWorld hw = service.getHelloWorld();
hw.sayHello();
// HelloWorldService
public class HelloWorldService {
private HelloWorld helloWorld;
public HelloWorldService(){
}
public void setHelloWorld(HelloWorld helloWorld){
this.helloWorld = helloWorld;
}
public HelloWorld = getHelloWorld() {
return this.helloWorld;
}
}
複製代碼
// beans.xml
<bean id="名稱" class="路徑"/>
<bean id="helloWorldService"
class="">
<property name="helloWorld" ref="springHelloWorld"/>
</bean>
複製代碼
spring
庫地址: http://maven.springframework.org/release/org/springframework/spring/
hello-world
:
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("Spring" + name);
}
}
複製代碼
// xml
<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-3.0.xsd">
<bean id="helloBean" class="">
<property name="name" value="dashu" />
</bean>
</beans>
複製代碼
// 執行
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloBean");
obj.printHello();
}
}
複製代碼
達叔小生:日後餘生,惟獨有你 You and me, we are family ! 90後帥氣小夥,良好的開發習慣;獨立思考的能力;主動而且善於溝通 簡書博客: 達叔小生 www.jianshu.com/u/c785ece60…