Spring是当今最流行的框架,今天开始,dayday同学要正式开始学习Spring了,加油
以下是一个简单的应用Spring框架的java程序
srcdaydayHelloSpring.java
package dayday;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by I am master on 2016/11/28.
*/
public class HelloSpring {
private String name;
public void setName(String name){
this.name=name;
}
public void println(){
System.out.println("hello "+name+" ,study Spring hard!!!");
}
}
srcdaydayMain.java
package dayday;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void main(String[] args){
//获得Spring容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
// 获得一个bean,spring中所有的java类都为bean
HelloSpring hellospring=ctx.getBean("hellospring",HelloSpring.class);
//调用类中的方法
hellospring.println();
// 原来不用spring框架的时候,将得到同样的运行结果
HelloSpring hellospring=new HelloSpring();
helloSpring.setName("dayday");
hellospring.println();
}
}
srceans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
//调用反射调用该类的无参构造函数创建一个bean(对象),必须要有包,id为创建bean(类)的名字
//name中的值(String)会相应的调用类中setString()的方法,value表示对应属性的值,也可以是ref,ref中是另外一个bean
<bean id="hellospring" class="dayday.HelloSpring">
<property name="name" value="dayday"/>
</bean>
//另外一种方式,通过p标签进行配置,如果要通过p标签配置的话,bean中不能含有有参构造函数
<!--通过p标签进行配置-->
<bean name="hellospring" class="dayday.HelloSpring" p:name="dayday" p:book-ref="book"/>
<bean name="book" class="dayday.Book" p:bookname="《Spring MVC》"/>
</beans>
运行结果
hello dayday ,study Spring hard!!!
<property>中使用ref
srcdaydayHelloSpring.java
package dayday;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by I am master on 2016/11/28.
*/
public class HelloSpring {
private Book book;
private String name;
public void setName(String name){
this.name=name;
}
public void setBook(Book book){
this.book=book;
}
public void println(){
System.out.println("hello "+name+" ,study Spring hard!!!"+book.print());
}
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
HelloSpring hellospring=ctx.getBean("hellospring",HelloSpring.class);
hellospring.println();
}
}
srcdaydayBook.java
package dayday;
/**
* Created by I am master on 2016/11/28.
*/
public class Book {
private String bookname;
public void setBookname(String bookname){
this.bookname=bookname;
}
public String print(){
return "read "+bookname;
}
}
srcdaydayMain.java
package dayday;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
HelloSpring hellospring=ctx.getBean("hellospring",HelloSpring.class);
hellospring.println();
}
}
srcdaydayeans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hellospring" class="dayday.HelloSpring">
<property name="name" value="dayday"/>
<property name="book" ref="book"></property>
</bean>
<bean id="book" class="dayday.Book">
<property name="bookname" value="《Spring mvc》"></property>
</bean>
</beans>
通过构造注入,之前的都为设值注入
//若通过构造注入,bean(java类)中必须要有构造函数
通过<constructor-arg.../>注入,value="String等基本类型" ref=“bean”
可以通过index属性指定构造参数值将作为第几个构造参数值,index="0"表明作为第一个构造函数参数值,错位开了
一般采用设值注入为主,构造注入为辅
srcdaydayHelloSpring.java
package dayday;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by I am master on 2016/11/28.
*/
public class HelloSpring {
private Book book;
private String name;
public HelloSpring(String name,Book book){
this.name=name;
this.book=book;
}
public void println(){
System.out.println("hello "+name+" ,study Spring hard!!!"+book.print());
}
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
HelloSpring hellospring=ctx.getBean("hellospring",HelloSpring.class);
hellospring.println();
}
}
srcdaydayBook.java
package dayday;
/**
* Created by I am master on 2016/11/28.
*/
public class Book {
private String bookname;
public Book(String bookname){
this.bookname=bookname;
}
public String print(){
return "read "+bookname;
}
}
srcdaydayMain.java
package dayday;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
HelloSpring hellospring=ctx.getBean("hellospring",HelloSpring.class);
hellospring.println();
}
}
srcdaydayeans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hellospring" class="dayday.HelloSpring">
<constructor-arg value="dayday"></constructor-arg>
<constructor-arg ref="book"></constructor-arg>
</bean>
<bean id="book" class="dayday.Book">
<constructor-arg value="《Spring MVC》"></constructor-arg>
</bean>
</beans>
bean作用域的配置
用scope属性来配置bean的作用域
Singleton:默认值,容器初始时创建bean实例
(在执行 <bean id="hellospring" class="dayday.HelloSpring">这句是调用无参构造函数创建)
在整个容器的生命周期内只创建这一个bean,单例的
常用的还有prototype,每次调用getbean()方法就调用构造函数创建一个bean