sping 快速入门

假如我们有两个包com.test 和 com.service 我们需要在com.test下面的Test.java 调用 com.service下面的UserService.java
 
传统的方法: 在test.java里面 导入UserService并使用
class UserService{
private String name;
public String getName(){
return name;
}
 
public void SetName(String newName){
name = newName;
}
 
public void sayHello(){
System.out.println("hello " + name);
}
}
----------------------------------------------------------------------------------------------------
package test
import com.service.UserService;
 
public Test{
public static void main(String[] args){
UserService service = new UserService();
service.setName("spring");
service.sayHello();
}
}
 
第二种 使用spring 来实现
使用spring的步骤
1. 下载spring 包
2. 在ide 中导入jar 包(最小配置是spring.jar 它包括了spring中常用的jar)
3.导入写日志的包 commons.logging
4. 创建一个spring的核心文件文件名必须是applicationContext.xml或者是spring-config.xml (hibernate 核心配置文件是hibernate.cfg.xml struts 的核心配置文件是struts-config.xml)该文件一般放在src 目录下,该文件引入xsd,可以在spring给出的案例
配置beans
5. 调用bean
 
package com.test;
import com.helloSpring.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
* Created by admin on 2017/12/3.
*/
public class SpringTest {
public static void main(String[] args){
// 1 得到spring 对象
ApplicationContext app = new ClassPathXmlApplicationContext("spring-config.xml");
UserService us = (UserService) app.getBean("service");
us.sayHelloSpring();
}
}
 
spring 调用有依赖的类。 比如我们的UserService类,假如里面还调用了一个GroupService
--------------------------------------------------------------------------------------------------------
package com.helloSpring;
 
/**
* Created by admin on 2017/12/3.
*/
public class UserService {
private String name;
private GroupService groupService;
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
public void sayHelloSpring(){
System.out.println("hello " + name);
groupService.sayHi();
}
 
public void setGroupService(GroupService groupService) {
this.groupService = groupService;
}
 
public GroupService getGroupService() {
return groupService;
}
}
 
--------------------------------------------------------------------------------------------------------
这种情况我们就需要设置UserService的bean和GroupService之间的依赖关系
配置如下:
<!-- 配置一条bean, bean 元素的作用是当spring加载的时候就会读取配置文件里面的bean元素标签,
spring会自动创建一个bean对象,并加载到内存 相当于UserService service = new UserService();-->
<bean id="service" class="com.helloSpring.UserService">
<!-- property 就是UserService 这个类的属性 value就是我们给属性赋值 相当于调用了UserService.setName-->
<property name="name">
<value>spring学习</value>
</property>
<!-- 设置引用属性 UserService 引用了GroupService-->
<property name="groupService" ref="groupService"/>
</bean>
<!-- 添加一个GroupService bean-->
<bean id="groupService" class="com.helloSpring.GroupService">
<property name="groupName">
<value>测试</value>
</property>
</bean>
 
 
传统方法和使用spring方法的区别
使用spring 并没有去new对象,创建对象的任务交给了spring
 
spring 是 一个容器框架,可以配置各种bean 并且维护各种bean之间的关系 当我们需要bean的时候之间getBean即可
 
 
 
 
 
posted @ 2018-01-13 17:25  晴天小猫  阅读(1898)  评论(0编辑  收藏  举报