Spring是当今最流行的框架,今天开始,dayday同学要正式开始学习Spring了,加油

以下是一个简单的应用Spring框架的java程序

src\dayday\HelloSpring.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!!!");
}
}

src\dayday\Main.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();
} }

src\beans.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
src\dayday\HelloSpring.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();
} }
src\dayday\Book.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;
}
}
src\dayday\Main.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();
} }
src\dayday\beans.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"表明作为第一个构造函数参数值,错位开了
一般采用设值注入为主,构造注入为辅
src\dayday\HelloSpring.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();
} }
 
src\dayday\Book.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;
}
}
 
src\dayday\Main.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();
} }
 
src\dayday\beans.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
												

初学Spring的更多相关文章

  1. 初学Spring有没有适合的书

    初学者之前没有阅读java框架源码的习惯.没有阅读过源码,知道整体流程么?知道依赖注入的概念么?知道aop么?知道其中用到了哪些设计模式么?再说了,如果一上手就是源码?难道你没有注意到Spring的类 ...

  2. 初学 Spring boot 报错 Whitelabel Error Page 404

    按照教程,写了个最简单的 HelloWorld,尼玛报错 -->Whitelabel Error Page 404. 网上99%都是项目结构不对,说什么 Application放在父级 pack ...

  3. 初学spring boot踩过的坑

    一.搭建spring boot环境 maven工程 pom文件内容 <project xmlns="http://maven.apache.org/POM/4.0.0" xm ...

  4. 记录初学Spring boot中使用GraphQL编写API的几种方式

    Spring boot+graphql 一.使用graphql-java-tools方式 <dependency> <groupId>com.graphql-java-kick ...

  5. 初学 spring

    1.spring 开发环境,包含eclipse https://spring.io/tools3/sts/all/

  6. 初学spring(二)

      1.spring推荐使用接口编程,配合di可以达到层与层之间解耦

  7. 初学spring(一)

    1.spring 在ssh 中所处的位置 struts 是web框架(jsp,action,actionform) hibernate 是orm框架,处于数据持久层 spring 是容器框架,配置be ...

  8. 初学spring之入门案列

    spring其实是一个很大的开源框架,而我学的就是spring framework,这只是spring其中的一小部分.有疑惑的可以去官网去看看,spring官网我就不提供了.一百度肯定有.和sprin ...

  9. 初学Spring的感觉

    1.使用接口 不同的类实现同一接口后都会变成同一类型的类.   spring作业1的思路 lab1: 建立一个有一个输出方法的接口类|并建一个英语类和一个数学类实现该接口.   这两个实现类都重写了那 ...

随机推荐

  1. laravel 5.2 引入第三方类

    composer 安装类依赖包 很受用 也很方便 但是要是一个有一定规模的公司技术团队 因为要照顾大局 还是引入类好些 下面是引入类的方法 1.首先在app目录下创建一个新的文件夹,命名Tools(可 ...

  2. Starting MySQL... ERROR! The server quit without updating PID file 解决办法

    来源:http://blog.rekfan.com/articles/186.html 我使用了第4条解决了问题 1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的 ...

  3. Linux下配置IP及安装vmware tool

    =======================CentOS 7以下======================= 配置IP: 1.获得mac地址 2.编辑ifcfg-eth0文件:vi /etc/sy ...

  4. ArcGIS GDB 文件中的lock文件影响复制

    复制或压缩gdb文件的时候,经常碰到有lock文件,解决方法是: 在catalog中停止相应的地图服务 如果用catalog预览过相应的地图,关闭catalog

  5. Tomcat环境配置

    1.安装java Path = 已有的值;%MYSQL%\bin;%CATALINA_HOME%\bin;%JAVA_HOME%\bin JAVA_HOME = C:\apps\Java\jdk1.8 ...

  6. 使用 AngularJS & NodeJS 实现基于token 的认证应用(转)

    认证是任何 web 应用中不可或缺的一部分.在这个教程中,我们会讨论基于 token 的认证系统以及它和传统的登录系统的不同.这篇教程的末尾,你会看到一个使用 AngularJS 和 NodeJS 构 ...

  7. oracle initialization or shutdown in progress解决方法

    [解决方法]   SQL> connect sys/hope as sysdba 已连接. SQL> shutdown normal ORA-01109: 数据库未打开     已经卸载数 ...

  8. 利用ajax向jsp传输数据

    ajax代码 var obtn=document.getElementsByTagName('input')[0]; obtn.onclick=function () { var xhr=null; ...

  9. [转]MYSQL高可用方案探究(总结)

    前言 http://blog.chinaunix.net/uid-20639775-id-3337432.htmlLvs+Keepalived+Mysql单点写入主主同步高可用方案 http://bl ...

  10. telnet 使用

    Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用telnet程序,用它连接 ...