spring_day01 demo代码
本博客為這篇博客的輔助博客,主要是其中自己寫的一些Demo.java代碼
目錄
日志的用法
Demo1.java
log4j.properties
控制反轉(zhuǎn)IOC的測(cè)試
Demo2.java
applicationContext.xml1
UserService.java
UserServiceImpl.java
依賴注入
CustomerDaoImpl.java
CustomerServiceImpl.java
Demo3.java
applicationContext.xml2
依賴注入的方式2:構(gòu)造方法
Car1.java
Person.java
Demo4.java
applicationContext.xml3
依賴注入方式3:p名稱空間的注入(純了解即可)
Car2.java
applicationContext.xml4
Demo4.java1
數(shù)組,集合(List,Set,Map),Properties等的注入
User.java
applicationContext.xml5
Demo4.java3
Spring框架的配置文件分開(kāi)管理(了解)
applicationContext.xml
applicationContext2.xml
Demo4.java4
Spring框架整合WEB(簡(jiǎn)單的Struts整合)
CustomerDao.java
CustomerDaoImpl.java
CustomerService.java
CustomerServiceImpl.java
CustomerAction.java
struts.xml
applicationContext.xml(ZH)
導(dǎo)包:spring-web-4.2.4.RELEASE.jar(spring依賴包)
web.xml
固定配置:
?
日志的用法
Demo1.java
package com.itheima.demo1; import org.apache.log4j.Logger; import org.junit.Test; /*** 演示日志用法* @author Administrator**/ public class Demo1 {//創(chuàng)建日志對(duì)象private Logger log=Logger.getLogger(Demo1.class);@Testpublic void run1(){System.out.println("執(zhí)行了...");log.info("執(zhí)行了...");} }log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c\:mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###### 在此配置文件里將inof改為off 則,所有的日志輸出都會(huì)失效,就像全部被注釋了一樣 ### 日志文件是spring依賴包里拷貝進(jìn)來(lái)的,默認(rèn)值就是info log4j.rootLogger=info, stdout控制反轉(zhuǎn)IOC的測(cè)試
Demo2.java
package com.itheima.demo2;import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource;/*** 測(cè)試IOC程序* @author Administrator**/ public class Demo2 {/*** 原來(lái)的方式*/@Testpublic void run1(){//創(chuàng)建實(shí)現(xiàn)類//UserServiceImpl usi = new UserServiceImpl();//usi.sayHello();UserService us=new UserServiceImpl();us.sayHello();}/*** 使用的是Spring框架的方式*/@Testpublic void run2(){//創(chuàng)建工廠,加載核心配置文件ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//一讀取配置文件,你配置的對(duì)象就全幫你配置完了//從工廠中獲取對(duì)象 參數(shù)就寫配置的id值UserService us = (UserService) ac.getBean("userService");//一般都強(qiáng)轉(zhuǎn)為接口 寫接口拓展容易 接口可以有多個(gè)實(shí)現(xiàn)類 都能被賦值//調(diào)用對(duì)象方法執(zhí)行us.sayHello();}/*** 老的工廠版本 過(guò)時(shí)了 不推薦用* BeanFactory*/@Testpublic void run4(){//創(chuàng)建工廠,加載核心配置文件BeanFactory factory =new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));//從工廠中獲取對(duì)象UserService us = (UserService) factory.getBean("userService");//老工廠 getBean時(shí)才會(huì)創(chuàng)建//調(diào)用對(duì)象的方法執(zhí)行us.sayHello(); }/*** 演示銷毀方法(了解)*/@Testpublic void run5(){//創(chuàng)建工廠,加載核心配置文件ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//一讀取配置文件,你配置的對(duì)象就全幫你配置完了//從工廠中獲取對(duì)象 參數(shù)就寫配置的id值UserService us = (UserService) ac.getBean("userService");//一般都強(qiáng)轉(zhuǎn)為接口 寫接口拓展容易 接口可以有多個(gè)實(shí)現(xiàn)類 都能被賦值//調(diào)用對(duì)象方法執(zhí)行us.sayHello(); //關(guān)閉工廠 里面的對(duì)象會(huì)全部銷毀ac.close();}//依賴注入 入門演示/*** 原始方式*/@Testpublic void run6_0(){//創(chuàng)建實(shí)現(xiàn)類UserServiceImpl usi = new UserServiceImpl();usi.setName("云天河");usi.sayHello();}/*** 依賴注入 : 不用set屬性值 直接配置屬性值*/@Testpublic void run6(){//創(chuàng)建工廠,加載核心配置文件ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//一讀取配置文件,你配置的對(duì)象就全幫你配置完了//從工廠中獲取對(duì)象 參數(shù)就寫配置的id值UserService us = (UserService) ac.getBean("userService");//一般都強(qiáng)轉(zhuǎn)為接口 寫接口拓展容易 接口可以有多個(gè)實(shí)現(xiàn)類 都能被賦值//調(diào)用對(duì)象方法執(zhí)行us.sayHello();}}applicationContext.xml1
<?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標(biāo)簽 --> <!-- class是實(shí)現(xiàn)類的全路徑(不是接口) id隨便取的名字 --><!-- <bean id="userService" class="com.itheima.demo2.UserServiceImpl" init-method="init" destroy-method="destory"/>--><!-- init-method="init" destroy-method="destory" (了解即可 需要用時(shí)來(lái)查 不需要記憶 只記憶重點(diǎn)的配置,獲得對(duì)象的方式下面依賴注入的第一種方式set,這種重點(diǎn))配置了之后 對(duì)象被創(chuàng)建時(shí)會(huì)執(zhí)行javaBean中的init()方法對(duì)象被銷毀時(shí)會(huì)創(chuàng)建javaBean中的destory()方法--><!-- 依賴注入 --><bean id="userService" class="com.itheima.demo2.UserServiceImpl"><!-- 注意name屬性需要有set方法 --><property name="name" value="云天河" /><!-- 創(chuàng)建對(duì)象時(shí)幫你把屬性輔助 --></bean></beans>UserService.java
package com.itheima.demo2;public interface UserService {public void sayHello(); }UserServiceImpl.java
package com.itheima.demo2;public class UserServiceImpl implements UserService {private String name;public void setName(String name) {this.name = name;}@Overridepublic void sayHello() {System.out.println("hello spring!! "+name);}//創(chuàng)建對(duì)象之前 框架會(huì)先調(diào)用init方法(前提配置了)public void init(){System.out.println("UserServiceImpl創(chuàng)建了...");}//銷毀時(shí)調(diào)用此方法public void destory(){System.out.println("UserServiceImpl銷毀了...");}}依賴注入
(也是最常用的一種方式 即set屬性方式)
CustomerDaoImpl.java
package com.itheima.demo3;public class CustomerDaoImpl {public void save(){System.out.println("我是持久層的dao....");} }CustomerServiceImpl.java
package com.itheima.demo3;public class CustomerServiceImpl {//不準(zhǔn)new了,那么必須要提供成員屬性讓框架幫你封裝了//提供成員屬性,提供set方法 (此類需要用到dao,這就是依賴)private CustomerDaoImpl customerDao;public void setCustomerDao(CustomerDaoImpl customerDao) {this.customerDao = customerDao;}public void save() {System.out.println("我是業(yè)務(wù)層的service.....");//原始的方式//new CustomerDaoImpl().save();//spring的方式customerDao.save();} }Demo3.java
package com.itheima.demo3;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo3 {/*** 原始方法*/@Testpublic void run1(){CustomerServiceImpl cs = new CustomerServiceImpl();cs.save();}/*** spring方式*/@Testpublic void run2(){ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");CustomerServiceImpl cs = (CustomerServiceImpl) ac.getBean("customerService");cs.save();} }applicationContext.xml2
<?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"><!-- 真正演示dao的依賴注入 --><!-- set方法的注入方式是標(biāo)準(zhǔn)常用的注入方式 --><bean id="customerDaoImpl" class="com.itheima.demo3.CustomerDaoImpl"/><bean id="customerService" class="com.itheima.demo3.CustomerServiceImpl"><property name="customerDao" ref="customerDaoImpl"/></bean> <!-- 上面的解釋:首先配置了兩個(gè)bean,那么框架就會(huì)幫你new相應(yīng)的類對(duì)象,然后service類需要用到dao類對(duì)象,則就要將配好的dao類注入到service類里 注意注入方式是引用上面配好的bean--></beans>依賴注入的方式2:構(gòu)造方法
注意普通屬性值參數(shù)用value,引用其他類對(duì)象則用ref
Car1.java
package com.itheima.demo4;public class Car1 {private String name;private Double price;//public Cart() {}//注意沒(méi)有空參構(gòu)造方法 配置時(shí)必須提供參數(shù)public Car1(String name, Double price) {super();this.name = name;this.price = price;}@Overridepublic String toString() {return "Car1 [name=" + name + ", price=" + price + "]";}}Person.java
package com.itheima.demo4;public class Person {private String name;private Car1 car1;//沒(méi)有內(nèi)存的引用 配置時(shí)不能寫value 要寫refpublic Person(String name, Car1 car1) {super();this.name = name;this.car1 = car1;}@Overridepublic String toString() {return "Person [name=" + name + ", car1=" + car1 + "]";}}Demo4.java
package com.itheima.demo4;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo4 {/*** 通過(guò)構(gòu)造方法實(shí)現(xiàn)注入 用得不多 標(biāo)準(zhǔn)用法是set方法*/@Testpublic void run1(){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");Car1 car1=(Car1) ac.getBean("car1");System.out.println(car1);}//補(bǔ)上 引用的配置@Testpublic void run2(){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");Person person=(Person) ac.getBean("person");System.out.println(person);}/*** 重復(fù)寫一個(gè)set方法注入方式 以強(qiáng)調(diào)這是標(biāo)準(zhǔn)的注入方式*/@Testpublic void run3(){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");Car2 car2= (Car2) ac.getBean("car2");System.out.println(car2);}}applicationContext.xml3
<?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"><!-- 演示構(gòu)造方法的注入方式 (不常用)--><bean id="car1" class="com.itheima.demo4.Car1"><!-- 配置構(gòu)造方法的參數(shù) 那么框架創(chuàng)建此類時(shí)就拿著參數(shù)調(diào)用對(duì)應(yīng)的構(gòu)造方法 --><!-- <constructor-arg name="name" value="奇瑞QQ"/><constructor-arg name="price" value="35000"/> --><!-- 另一種配置參數(shù)的方式 index="0"表示第一個(gè)構(gòu)造參數(shù) ="1"表示第二個(gè)構(gòu)造參數(shù)--><constructor-arg index="0" value="奇瑞QQ2"/><constructor-arg index="1" value="50000"/></bean><bean id="person" class="com.itheima.demo4.Person"><constructor-arg name="name" value="天河"/><constructor-arg name="car1" ref="car1"/><!-- 注意引用類實(shí)例 不能寫value--></bean> <!-- 重復(fù)演示set方法注入方式 --><bean id="car2" class="com.itheima.demo4.Car2"><property name="name" value="飛車"/><property name="price" value="99999"/></bean></beans>?
依賴注入方式3:p名稱空間的注入(純了解即可)
Car2.java
package com.itheima.demo4;public class Car2 {private String name;private Double price;public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}//方法多了沒(méi)事 少了不行@Overridepublic String toString() {return "Car2 [name=" + name + ", price=" + price + "]";}}applicationContext.xml4
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 采用p名稱空間的方式注入(了解) 引入名稱空間時(shí)為了避免同名屬性沖突,去了別名p 下面也要加上別名p:--><!-- <bean id="car2" class="com.itheima.demo4.Car2" p:name="保時(shí)捷" p:price="1000"/> --><!-- 一行即可 也很簡(jiǎn)單 --><!-- SpEL表達(dá)式方式注入 --><bean id="car2" class="com.itheima.demo4.Car2"><property name="name" value="#{'飛車2'}"/><property name="price" value="#{99999}"/></bean><!-- 這種方式的好處沒(méi)演示出來(lái) 可以直接像el表達(dá)式一樣用'.'調(diào)用屬性 --></beans>Demo4.java1
@Testpublic void run3(){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");Car2 car2= (Car2) ac.getBean("car2");System.out.println(car2);}?
數(shù)組,集合(List,Set,Map),Properties等的注入
自己寫的不多,但是三大框架整合時(shí)需要用到這些配置
User.java
package com.itheima.demo4;import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties;public class User {private String[] arrs;public void setArrs(String[] arrs) {this.arrs = arrs;}private List<String> list;public void setList(List<String> list) {this.list = list;}private Map<String, String> map;public void setMap(Map<String, String> map) {this.map = map;}private Properties pro;//java.utils包下的 屬性文件類public void setPro(Properties pro) {this.pro = pro;}@Overridepublic String toString() {return "User [arrs=" + Arrays.toString(arrs) + ", list=" + list+ ", map=" + map + ", pro=" + pro + "]";} }applicationContext.xml5
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 注入集合 --><bean id="user" class="com.itheima.demo4.User"><property name="arrs"><!-- 數(shù)組或者集合的值需要在list子標(biāo)簽里配置了 --><list><value>天河</value><value>紫英</value><value>夢(mèng)璃</value><value>菱紗</value></list></property><property name="list"><list><value>玄霄</value><value>天青</value><value>夙玉</value><!-- <ref="person"/> 同樣list中放的是對(duì)象的時(shí)候value換成ref--></list></property><!-- set集合配置是 <list>改成<set> --><property name="map"><map><!-- key是對(duì)象時(shí)用key-ref value是對(duì)象時(shí)用value-ref --><entry key="a" value="蘋果"/> <entry key="b" value="香蕉"/> </map></property><!-- 屬性文件類也只是一個(gè)普通的屬性 配置沒(méi)太多特殊 直接在這里寫屬性文件的內(nèi)容罷了 --><property name="pro"><props><prop key="username">root</prop><prop key="password">1234</prop></props></property></bean></beans>Demo4.java3
package com.itheima.demo4;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo4 { /*** 測(cè)試注入集合*/@Testpublic void run4(){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");User user= (User) ac.getBean("user");System.out.println(user);}}?
Spring框架的配置文件分開(kāi)管理(了解)
applicationContext.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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 注入集合 --><!-- <bean id="user" class="com.itheima.demo4.User"><property name="arrs">數(shù)組或者集合的值需要在list子標(biāo)簽里配置了<list><value>天河</value><value>紫英</value><value>夢(mèng)璃</value><value>菱紗</value></list></property><property name="list"><list><value>玄霄</value><value>天青</value><value>夙玉</value><ref="person"/> 同樣list中放的是對(duì)象的時(shí)候value換成ref</list></property>set集合配置是 <list>改成<set><property name="map"><map>key是對(duì)象時(shí)用key-ref value是對(duì)象時(shí)用value-ref<entry key="a" value="蘋果"/> <entry key="b" value="香蕉"/> </map></property>屬性文件類也只是一個(gè)普通的屬性 配置沒(méi)太多特殊 直接在這里寫屬性文件的內(nèi)容罷了<property name="pro"><props><prop key="username">root</prop><prop key="password">1234</prop></props></property></bean> --><!-- 配置文件寫多個(gè)(多寫集合從配置文件) 分模塊寫 方便管理 但是要在主配置文件中引入從配置文件 --><import resource="applicationContext2.xml"/><!-- 同一個(gè)目錄下直接寫相對(duì)路徑 在某個(gè)包寫要加完整包名 com/itheima/xxx/applicationContext2.xml 和struts一樣 --></beans>applicationContext2.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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 注入集合 --><bean id="user" class="com.itheima.demo4.User"><property name="arrs"><!-- 數(shù)組或者集合的值需要在list子標(biāo)簽里配置了 --><list><value>天河</value><value>紫英</value><value>夢(mèng)璃</value><value>菱紗</value></list></property><property name="list"><list><value>玄霄</value><value>天青</value><value>夙玉</value><!-- <ref="person"/> 同樣list中放的是對(duì)象的時(shí)候value換成ref--></list></property><!-- set集合配置是 <list>改成<set> --><property name="map"><map><!-- key是對(duì)象時(shí)用key-ref value是對(duì)象時(shí)用value-ref --><entry key="a" value="蘋果"/> <entry key="b" value="香蕉"/> </map></property><!-- 屬性文件類也只是一個(gè)普通的屬性 配置沒(méi)太多特殊 直接在這里寫屬性文件的內(nèi)容罷了 --><property name="pro"><props><prop key="username">root</prop><prop key="password">1234</prop></props></property></bean></beans>Demo4.java4
package com.itheima.demo4;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo4 {/*** 測(cè)試配置文件分模塊管理*/@Testpublic void run4(){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");User user= (User) ac.getBean("user");System.out.println(user);}/*** 可以直接加載多個(gè)多個(gè)配置文件 此時(shí)就不必在主配置文件中引入其他配置文件了* 但是要是真的有多個(gè)配置文件 還是建議引入而不寫這種代碼 (傾向于在配置文件中寫)*/@Testpublic void run5(){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");User user= (User) ac.getBean("user");System.out.println(user);}}?
Spring框架整合WEB(簡(jiǎn)單的Struts整合)
主要解決apllication.xml配置文件如何只加載一次的問(wèn)題
先搭建struts2環(huán)境(核心過(guò)濾器別忘了),再搭建spring最簡(jiǎn)單的環(huán)境(6個(gè)包)
CustomerDao.java
package com.itheima.dao;public interface CustomerDao {public void save(); }CustomerDaoImpl.java
package com.itheima.dao;import com.itheima.service.CustomerService;public class CustomerDaoImpl implements CustomerService{@Overridepublic void save() {System.out.println("持久層:保存客戶...");}}CustomerService.java
package com.itheima.service;public interface CustomerService {public void save(); }CustomerServiceImpl.java
package com.itheima.service;import com.itheima.dao.CustomerDaoImpl;public class CustomerServiceImpl implements CustomerService {private CustomerDaoImpl customerDao;public void setCustomerDao(CustomerDaoImpl customerDao) {this.customerDao = customerDao;}@Overridepublic void save() {System.out.println("業(yè)務(wù)層:保存客戶...");customerDao.save();}}CustomerAction.java
package com.itheima.web.action;import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils;import com.itheima.service.CustomerService; import com.opensymphony.xwork2.ActionSupport;/*** 客戶的Action* @author Administrator**/ public class CustomerAction extends ActionSupport{//spring學(xué)完后 實(shí)戰(zhàn)之前 將所有博客和代碼讀一遍 復(fù)習(xí)一下下 最好有個(gè)小總結(jié) 簡(jiǎn)單復(fù)習(xí)而已 不要苛求什么/*** 保存客戶* @return*/public String save(){System.out.println("WEB層保存客戶...");/*//使用工廠 這樣寫每次請(qǐng)求action都會(huì)加載一次配置文件,也即都會(huì)創(chuàng)建一個(gè)工廠,很明顯這不合適 這個(gè)時(shí)候就需要整合操作了ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");CustomerService cs= (CustomerService) ac.getBean("customerService");cs.save();return NONE;*//*** 那么需要導(dǎo)包 spring-web... 然后監(jiān)聽(tīng)ServletContext類(最大的全局共享域)* 這個(gè)類是在服務(wù)器開(kāi)啟時(shí)創(chuàng)建,關(guān)閉時(shí)銷毀* 有個(gè)專門的監(jiān)聽(tīng)器是監(jiān)聽(tīng)這個(gè)類的創(chuàng)建和銷毀的* * 過(guò)程如下:啟動(dòng)服務(wù)器->ServletContext對(duì)象被創(chuàng)建->ServletContext監(jiān)聽(tīng)器對(duì)象方法執(zhí)行->在監(jiān)聽(tīng)器里執(zhí)行加載配置文件的代碼* ->那么配置文件只會(huì)加載一次,里面所有的類對(duì)象也都創(chuàng)建好了* * spring-web-4.2.4.RELEASE.jar包里就有監(jiān)聽(tīng)器 配置一下即可*///解決方案:需要使用web的工廠方式//action里獲得servletContext有個(gè)好值棧啊ServletContext servletContext = ServletActionContext.getServletContext();//獲得web工廠WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);//利用web工廠獲得對(duì)象CustomerService cs= (CustomerService)context.getBean("customerService");cs.save();return NONE;} }struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 包結(jié)構(gòu) 包名隨便取 但是不能相同--><package name="crm" namespace="/" extends="struts-default"><action name="customer_*" class="com.itheima.web.action.CustomerAction" method="{1}"/><!--{1}取得是前面name屬性中第1個(gè)* --></package></struts>applicationContext.xml(ZH)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置客戶的業(yè)務(wù)層 --><bean id="customerService" class="com.itheima.service.CustomerServiceImpl"><property name="customerDao" ref="customerDao"/></bean><!-- 配置客戶的dao --><bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl"/></beans>導(dǎo)包:spring-web-4.2.4.RELEASE.jar(spring依賴包)
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>day35_crm</display-name><!-- 配置web整合的監(jiān)聽(tīng)器(最原始的Listener) --><listener><!-- 此監(jiān)聽(tīng)器默認(rèn)情況下只能加載WEB-INF目錄下的配置文件 --><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 提供配置方式,讓他加載src目錄下的配置文件(配置一個(gè)指定的全局參數(shù)) --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 核心過(guò)濾器千萬(wàn)別忘記了 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>固定配置:
<!-- 配置web整合的監(jiān)聽(tīng)器(最原始的Listener) --><listener><!-- 此監(jiān)聽(tīng)器默認(rèn)情況下只能加載WEB-INF目錄下的配置文件 --><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 提供配置方式,讓他加載src目錄下的配置文件(配置一個(gè)指定的全局參數(shù)) --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- struts2核心過(guò)濾器千萬(wàn)別忘記了 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>?
總結(jié)
以上是生活随笔為你收集整理的spring_day01 demo代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【实验2 选择结构】7-3 sdut-C
- 下一篇: ABAQUS学习之路