spring框架
Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性的角度而言,绝大部分Java应用都可以从Spring中受益。
◆目的:解决企业应用开发的复杂性
◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
◆范围:任何Java应用
Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。
例1:方向生成spring
hello.java
package bean; public class Hello { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String sayHello(){ return "Hello!"+message; } }
applicationContext.xml(生成spring的XML文件),把hello.java加到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-3.1.xsd"> <bean name="hello1" class="bean.Hello"> <property name="message" value="spring"></property> </bean> </beans>
测试Test.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import bean.Hello; public class Test { public static void main(String[] args) { /** * application context构造器通常使用字符串或字符串数组作为资源 * (比如组成context定义 的XML文件)的定位路径。 */ ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); Hello hello = (Hello) context.getBean("hello1");//去找xml文件中bean的name System.out.println(hello.sayHello()); } }
例2:
加spring的jar包:
接口InK.java
package bean; public interface InK { //设定墨水颜色 public String getColer(); }
package bean; public interface Paper { //设定尺寸 public String getSize(); //纸张的厂商 public String getPub(); }
package bean; public class A4Paper implements Paper{ @Override public String getSize() { // TODO Auto-generated method stub return "A4"; } @Override public String getPub() { // TODO Auto-generated method stub return "csdn"; } }
package bean; public class B5Paper implements Paper{ @Override public String getSize() { // TODO Auto-generated method stub return "B5"; } @Override public String getPub() { // TODO Auto-generated method stub return "java"; } }
package bean; public class BlackInk implements InK{ @Override public String getColer() { return "caise墨水"; } }
package bean; public class ColorInk implements InK{ @Override public String getColer() { return "黑白墨水"; } }
Printer.java
package bean; public class Printer { private InK inK; private Paper paper; private float price; //打印方法 public void print(String str){ System.out.println("用这个颜色的墨水"+inK.getColer()); System.out.println("用那个厂商尺寸的纸"+paper.getSize()); System.out.println("用那个厂商的纸"+paper.getPub()); System.out.println(str); } //注入方式3中 //1.构造函数 参数生命周期等同对象的声明周期 public Printer() { super(); // TODO Auto-generated constructor stub } public Printer(InK inK, Paper paper,float price) { super(); this.inK = inK; this.paper = paper; this.price=price;//******通过基本数据类型,注入price*******必须写在构造方法中 } //2.通过一般方法注入,导致参数设定受限制 public void init(InK inK, Paper paper) { this.inK = inK; this.paper = paper; } //3.set get使用灵活,但是繁琐 public InK getInK() { return inK; } public void setInK(InK inK) { this.inK = inK; } public Paper getPaper() { return paper; } public void setPaper(Paper paper) { this.paper = paper; } }
分别测试输出这三中方法
/* * InK ink = new ColorInk(); Paper paper = new B5Paper(); //1. //Printer * print = new Printer(ink, paper); //print.print("打印机测试"); * * //2. Printer print=new Printer(); print.init(ink, paper); * print.print("打印机测试"); * * //3. Printer print=new Printer(); print.setInK(ink); * print.setPaper(paper); print.print("打印机测试"); */
// 早期的方式创建资源 Resource res=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(res); Printer printer=factory.getBean("printer",Printer.class); printer.print("答应测试");
<!-- 国际化必须配置 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>mess</value>//国际标签的名字,可以配写多个 </list> </property> </bean>
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-4.0.xsd"> <bean name="inK" class="bean.BlackInk"></bean> <bean name="paper" class="bean.B5Paper"></bean> <!-- 多例一般是线程安全的 scope="prototype"才使用, 一般都是单例模式 --> <!--通过get和set方法注入 --> <bean name="printer" class="bean.Printer"> <property name="inK"> <ref bean="inK" /> </property> <property name="paper"> <ref bean="paper" /> </property> </bean> <!-- 构造函数的调用 --> <bean name="print" class="bean.Printer"> <constructor-arg> <ref bean="inK" /> </constructor-arg> <constructor-arg> <ref bean="paper" /> </constructor-arg> <!-- 通过基本数据类型 --> <constructor-arg> <value>2222.0</value> </constructor-arg> </bean> <bean name="printer2" class="bean.Printer"> <property name="inK"> <ref bean="inK" /> </property> <property name="paper"> <ref bean="paper" /> </property> </bean> </beans>
测试代码(如果在XML文件中有两个Printer.Class必须指定唯一name的Printer printer = context.getBean("printer",Printer.class) printer)
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); // Printer printer=context.getBean(Printer.class); // 两个bean在applicationContext.xml /** * <bean name="printer" class="bean.Printer"> <property name="inK"> <ref * bean="inK" /> </property> <property name="paper"> <ref bean="paper" * /> </property> </bean> <bean name="printer1" class="bean.Printer"> * <property name="inK"> <ref bean="inK" /> </property> <property * name="paper"> <ref bean="paper" /> </property> </bean> */ //Printer printer = context.getBean(Printer.class);//会报错,必须指定唯一的 Printer printer = context.getBean("printer",Printer.class); printer.print("答应测试");
声明多个XML文件bean.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-4.0.xsd"> <bean name="ink1" class="bean.ColorInk"></bean> <bean name="paper1" class="bean.A4Paper"></bean> <bean name="printer1" class="bean.Printer"> <property name="inK"> <ref bean="ink1" /> </property> <property name="paper"> <ref bean="paper1" /> </property> </bean> <!-- 国际化必须配置 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>mess</value> </list> </property> </bean> </beans>
<bean name="paper1" class="bean.A4Paper"></bean>
<bean name="printer1" class="bean.Printer">
<property name="paper">
<ref bean="paper1" />
</property>
</bean>
<bean name="printer1" class="bean.Printer">
<property name="paper">
<ref bean="paper1" />
</property>
</bean>
ref里的bean名称与name相等,property的name为类中的名称
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"bean.xml","applicationContext.xml"}); Printer printer=ctx.getBean("printer",Printer.class); Printer printer1=ctx.getBean("printer1",Printer.class); printer1.print("aaa"); printer.print("datiny");
mess.properties
hello=welcome,{0} now=now is\:{0}测试代码
//国际化 参数1配置文件key 参数2占位符参数 参数3语言环境locale ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","bean.xml"}); String hello=ctx.getMessage("hello",new Object[]{"spring"},Locale.getDefault()); System.out.println(hello); String hello1=ctx.getMessage("now",new Object[]{new Date()},Locale.getDefault()); System.out.println(hello1);
事件必须在XML文件配置
<!-- 配置自定义事件EmailEvent的监听器 --> <bean class="bean.EmailListener" name="email" />
EmailEvent.java
package bean; import org.springframework.context.ApplicationEvent; public class EmailEvent extends ApplicationEvent{ private static final long serialVersionUID = 1L; private String address; private String text; //source事件源 public EmailEvent(Object source) { super(source); } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getText() { return text; } public void setText(String text) { this.text = text; } public EmailEvent(Object source, String address, String text) { super(source); this.address = address; this.text = text; } }
EmailListener.java
package bean; import org.springframework.context.ApplicationListener; //监听器 public class EmailListener implements ApplicationListener<EmailEvent> { @Override public void onApplicationEvent(EmailEvent arg0) { System.out.println(arg0.getAddress()); System.out.println(arg0.getText()); System.out.println(arg0.getSource()); System.out.println(arg0.getTimestamp()); } }
/* * 手动抛事件 */ //先绑定事件 ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"bean.xml","applicationContext.xml"}); Printer printer=ctx.getBean("print",Printer.class); EmailEvent event=new EmailEvent(printer,"csdn","csdn"); ctx.publishEvent(event);