spring(一)

1、spring是什么

struts是web框架(jsp/action/actionfrom)

hibemate 是 orm框架,处于持久层

spring 是容器框架,用于配置bean,并维护bean之间关系的框架。

bean是spring中很重要的概念,bean可以是Java中任意的一种对象,比如:Javabean/service/数据源/dao/ioc(控制反转)/di(依赖注入)

2、spring层次图

3、开发一个spring项目

(1)引包,最小配置:spring.jar 和 commons-logging.jar

(2)创建spring的核心文件,applicationContext.xml

[ hibermate 核心文件 hibermate.cfg.xml

struts 核心文件 struts-config.xml ]

实例:

 1 package com.service;
 2 
 3 public class UserService {
 4     private String name;
 5 
 6     public String getName() {
 7         return name;
 8     }
 9 
10     public void setName(String name) {
11         this.name = name;
12     }
13     public void sayHello() {
14         System.out.println("Hello " + name);
15     }
16 }

 

不使用spring:

 1 package com.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 import org.w3c.dom.UserDataHandler;
 7 
 8 import com.service.UserService;
 9 
10 public class Test {
11     public static void main(String[] args) {
12     
13     
14         //传统方法
15         UserService userService = new UserService();
16         userService.setName("张三");
17         userService.sayHello();
18     }
19 }

运行结果:

 

 

 

使用spring

 1 package com.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 import org.w3c.dom.UserDataHandler;
 7 
 8 import com.service.UserService;
 9 
10 public class Test {
11     public static void main(String[] args) {
12 
13         
14         //spring方法
15         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
16         UserService us = (UserService) ac.getBean("userService");
17         us.sayHello();
18     }
19 
20 }

applicationContext.xml 中代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:jee="http://www.springframework.org/schema/jee"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:context="http://www.springframework.org/schema/context"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
 9     default-lazy-init="false">
10 
11     
12 <!-- 在容器中配置bean(service/dao/domain/action/数据源) -->
13 <!-- bean作用是 当spring框架加载时,spring会自动创建一个bean对象,并放入内存 -->
14 <bean id="userService" class="com.service.UserService">
15      <property name="name">
16          <value>张三</value>
17      </property>
18 </bean>
19     
20 </beans>

运行结果:

 

posted @ 2017-05-09 11:33  青萍之末  阅读(123)  评论(0)    收藏  举报