Spring 5.0 在eclipse上的配置


     Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transaction Management,等等..

 下面讲一下具体的配置问题:

1、首先去官网下载 它的jar包 http://repo.spring.io/release/org/springframework/spring/ 我们用的是 5.0.1;

2、建一个 java 项目 在 lib 文件夹下 把jar 包全部复制进去,并添加到项目路径下。


3、在 src 文件夹下创建一个 xml文件 


4、命名为: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">
<alias name="hello" alias="he"/>




<!-- 加载类并进行配置 -->
<bean class="com.bean.User" id="user"  p:id="123" >


</bean>



</beans>


5、创建包  和类


6、代码如下:

package com.bean;


public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(){
System.out.println("无参构造方法");
};
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
System.out.println("有参构造方法");
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + "]";
}




}




package test;






import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bean.User;


public class HelloTest {


public static void main(String[] args) {


ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)context.getBean("user");
System.out.println(user);
}


}

7、运行结果: