Spring框架基本代码
1.准备阶段:

2.基本引入:
接口:
package com.xk.spring.kp01_hello;
public interface IHello {
public void nice();
}
//实现类:
package com.xk.spring.kp01_hello;
public class Hello implements IHello {
public void nice() {
System.out.println("Hello.nice()");
} }
//测试类:
package com.xk.spring.kp01_hello;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
@SuppressWarnings("deprecation")
public class TestHello {
// 不采用@Autowired注解,不采用自动装配.
// 采用面向对象编程思路.
ClassPathResource resource = new ClassPathResource("HelloSpring.xml");
BeanFactory factory = new XmlBeanFactory(resource);
IHello hello = new Hello();
@Test
public void testName() throws Exception {
IHello bean = factory.getBean("HelloSpringBean", IHello.class);
bean.nice();
System.out.println("~~~~~~~~~~~~~~~~~~~");
hello.nice();
} }
2. 基本模型:
//接口:
package com.xk.spring.kp02_modules;
public interface ITestmoudles {
public void sayhello();
}
//实现类:
package com.xk.spring.kp02_modules; public class TestMoudles implements ITestmoudles { public void sayhello() {
System.out.println("TestMoudles.sayhello()");
} public void init() {
System.out.println("TestMoudles.init()");
} public void destroy() {
System.out.println("TestMoudles.destroy()");
} }
<!-- 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="ModulesBean" class="com.xk.spring.modules.TestMoudles" init-method="init"
destroy-method="destroy"/> </beans>
//测试类
package com.xk.spring.kp02_modules;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//面向接口编程
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
// @ContextConfiguration("xml的名字.xml")如果有,则xml的名字不需要是测试类-context
public class TestMoudlesTest {
/*
ClassPathResource resource = new ClassPathResource("Moudles.xml");
BeanFactory factory = new XmlBeanFactory(resource);
*/
@Autowired //不用new实现类 完全面向接口编程
BeanFactory factory; @Test // 测试
public void testName() throws Exception {
ITestmoudles bean = factory.getBean("ModulesBean", ITestmoudles.class);
bean.sayhello(); }
}
3.基本传统实现创建bean:
package com.xk.spring.kp03_container.s1_basic;
public interface IContainer {
public void testcontainer();
}
//实现类:
package com.xk.spring.kp03_container.s1_basic; public class ContainerImlp implements IContainer { public ContainerImlp() {
System.out.println("ContainerImlp.ContainerImlp()");
} public void init() {
System.out.println("ContainerImlp.init()");
} public void destroy() {
System.out.println("ContainerImlp.destory()");
} public void testcontainer() {
System.out.println("-------------------------");
} }
<!-- 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">
<!-- id= "唯一标识" class= "实现类的权限定"-->
<bean id="testContainerBean" class="com.xk.spring.kp03_container.s1_basic.ContainerImlp"
init-method="init" destroy-method="destroy"/> <!-- lazy-init:true 告诉Spring容器, 不再在容器启动的时候实例化对应bean --> </beans>
注:运行时将第一行注释删掉
//测试类:
package com.xk.spring.kp03_container.s1_basic; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ContainerTest {
//ApplicationContext: 也是一个接口, 继承了BeanFactory, ApplicationContext容器功能更加全面
@Autowired
ApplicationContext appCtx;
@Test
public void testName() throws Exception {
IContainer con = appCtx.getBean("testContainerBean", IContainer.class);
con.testcontainer();
}
}
4.静态工厂方法生产bean:
package com.xk.spring.kp03_container.s2_staticcreatebean;
public interface IStaticCreateBean {
public void creatbean();
}
package com.xk.spring.kp03_container.s2_staticcreatebean;
public class StaticCreateBeanImpl implements IStaticCreateBean {
@Override
public void creatbean() {
System.out.println("------------------------");
}
public void init() {
System.out.println("StaticCreateBeanImpl.init()");
}
public void destroy() {
System.out.println("StaticCreateBeanImpl.destroy()");
}
}
package com.xk.spring.kp03_container.s2_staticcreatebean;
public class StaticCreateBeanFactory {
public static StaticCreateBeanImpl creatbean() {
StaticCreateBeanImpl bean = new StaticCreateBeanImpl();
bean.init();
bean.destroy();
bean.creatbean();
return bean;
}
}
package com.xk.spring.kp03_container.s2_staticcreatebean; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class StaticCreateBeanTest { @Autowired
ApplicationContext appCtx; @Test
public void testName() throws Exception {
IStaticCreateBean bean = appCtx.getBean("StaticCreateBean", IStaticCreateBean.class);
bean.creatbean();
} }
<?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="StaticCreateBean"
class="com.xk.spring.kp03_container.s2_staticcreatebean.StaticCreateBeanFactory"
init-method="init" destroy-method="destroy" factory-method="creatbean" />
</beans>
5.BeanFactory:
package com.xk.spring.kp03_container.s3_factorybeancreate;
public interface IFactoryCreateBean {
public void factory();
}
package com.xk.spring.kp03_container.s3_factorybeancreate;
public class ImplFactoryCreateBean implements IFactoryCreateBean {
public ImplFactoryCreateBean() {
System.out.println("ImplFactoryBeanCreate.ImplFactoryBeanCreate()");
}
public void init() {
System.out.println("ImplFactoryBeanCreate.init()");
}
public void destory(){
System.out.println("ImplFactoryBeanCreate.destory()");
}
@Override
public void factory() {
System.out.println("ImplFactoryBeanCreate.factory()");
}
}
package com.xk.spring.kp03_container.s3_factorybeancreate; import org.springframework.beans.factory.FactoryBean; //创建bean工厂,只需要一个类实现FactoryBean接口,则Spring框架就会默认为该类是一个工厂类
public class CreateBeanFactory implements FactoryBean<ImplFactoryCreateBean> { @Override
public ImplFactoryCreateBean getObject() throws Exception {
ImplFactoryCreateBean bean = new ImplFactoryCreateBean();
return bean;
} @Override
public Class<?> getObjectType() {
return ImplFactoryCreateBean.class;
} @Override
public boolean isSingleton() {
return false;
} }
package com.xk.spring.kp03_container.s3_factorybeancreate; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class FactoryBeanTest {
/*
* 自动配置
*
* @Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,
* 这里必须明确:@Autowired是根据类型进行自动装配的,如果需要按名称进行装配,则需要配合@Qualifier[1]使用.
* @Autowired标注可以放在成员变量上,也可以放在成员变量的set方法上。前者,
* Spring会直接将UserDao类型的唯一一个bean赋值给userDao这个成员变量;后者,
* Spring会调用setUserDao方法来将UserDao类型的唯一一个bean装配到userDao这个属性。 Spring
* 2.5引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
* 通过 @Autowired的使用来消除set ,get方法。
*/
@Autowired
ApplicationContext appCtx; @Test
public void FactoryBean() throws Exception {
ImplFactoryCreateBean bean = appCtx.getBean("CreateFactoryBean", ImplFactoryCreateBean.class);
bean.factory();
} }
<?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="CreateFactoryBean"
class="com.xk.spring.kp03_container.s3_factorybeancreate.ImplFactoryCreateBean"
init-method="init" destroy-method="factory" /> </beans>
6:instance实例化创建Bean:
package com.xk.spring.kp03_container.s4_instancecreatebeanFactory;
public interface IInstanceCreateBean {
public void instanceCreate();
}
package com.xk.spring.kp03_container.s4_instancecreatebeanFactory;
public class ImplInatanceCreateBean implements IInstanceCreateBean {
public ImplInatanceCreateBean() {
System.out.println("我是构造......");
}
public void init() {
System.out.println("我是init......");
}
public void destroy() {
System.out.println("我是destroy......");
}
@Override
public void instanceCreate() {
System.out.println("我是实现方法.......");
}
}
package com.xk.spring.kp03_container.s4_instancecreatebeanFactory;
public class InstanceCreateFactoryBean {
public ImplInatanceCreateBean create() {
ImplInatanceCreateBean bean = new ImplInatanceCreateBean();
bean.init();
bean.destroy();
return bean;
}
}
package com.xk.spring.kp03_container.s4_instancecreatebeanFactory; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class InstanceCreateFactoryTest { @Autowired
ApplicationContext AppCtx; @Test
public void InstanceFactory() throws Exception {
IInstanceCreateBean bean = AppCtx.getBean("InstanceCreateFactoryBean", IInstanceCreateBean.class);
bean.instanceCreate();
} }
<?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="InstanceCreateFactoryBean"
class="com.xk.spring.kp03_container.s4_instancecreatebeanFactory.ImplInatanceCreateBean"
init-method="init" destroy-method="destroy"/> </beans>
7.Scope属性使用:
package com.xk.spring.kp03_container.s5_scope;
public interface IScopeBean {
public void init();
public void destroy();
public void scope();
}
package com.xk.spring.kp03_container.s5_scope;
public class ImplScopeBean implements IScopeBean {
public ImplScopeBean() {
System.out.println("我是构造...");
}
@Override
public void init() {
System.out.println("init...");
}
@Override
public void destroy() {
System.out.println("destroy...");
}
@Override
public void scope() {
System.out.println("scope...");
}
}
package com.xk.spring.kp03_container.s5_scope; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ScopeBeanTest {
@Autowired
ApplicationContext AppCtx; @Test
public void Scope() throws Exception {
IScopeBean bean1 = AppCtx.getBean("ScopeBean", IScopeBean.class);
IScopeBean bean2 = AppCtx.getBean("ScopeBean", IScopeBean.class); System.out.println(bean1==bean2);
bean1.init();
bean2.init();
bean1.destroy();
bean1.scope();
}
}
<?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="ScopeBean" class="com.xk.spring.kp03_container.s5_scope.ImplScopeBean"
init-method="init" destroy-method="destroy" scope="singleton" />
<!-- scope: singleton: 单例 每次创建的bean相同;
prototype: 原型类型, 非单例的, 每次getBean都会创建一个新的对象
-->
</beans>
8.Spring和Struts2练习使用:
工程结构:

package com.xk.spring.exercise.dao;
import com.xk.spring.exercise.domain.Student;
public interface IStudentDao {
public void save(Student stu);
}
package com.xk.spring.exercise.dao.impl; import org.springframework.stereotype.Repository; import com.xk.spring.exercise.dao.IStudentDao;
import com.xk.spring.exercise.domain.Student;
@Repository
public class StudentImpl implements IStudentDao {
@Override
public void save(Student stu) {
System.out.println("保存中..."+stu);
} }
//domain
package com.xk.spring.exercise.domain; public class Student {
private String name;
private int id; public Student() {
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Override
public String toString() {
return "Student [name=" + name + ", id=" + id + "]";
} }
//action层
package com.xk.spring.exercise.action; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import com.xk.spring.exercise.domain.Student;
import com.xk.spring.exercise.service.IServiceStudent;
@Controller
public class StudentAction {
@Autowired
private IServiceStudent service; public void save() {
Student stud = new Student();
stud.setName("张三");
stud.setId(10); service.save(stud);
}
}
//Servixe层
//接口
package com.xk.spring.exercise.service; import com.xk.spring.exercise.domain.Student; public interface IServiceStudent {
public void save (Student stu);
}
//实现类:
package com.xk.spring.exercise.service.serviceimpl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.xk.spring.exercise.dao.IStudentDao;
import com.xk.spring.exercise.domain.Student;
import com.xk.spring.exercise.service.IServiceStudent;
@Service
public class ServiceStudentImpl implements IServiceStudent {
//需要一个dao的实例;
@Autowired
private IStudentDao dao; @Override
public void save(Student stu) {
dao.save(stu);
} }
//测试类:
package com.xk.spring.exercise.actionTest; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.xk.spring.exercise.action.StudentAction;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ActionTest { @Autowired
StudentAction student;
@Test
public void testActionTest() throws Exception { student.save(); }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.xk.spring.exercise"/> </beans>

Spring框架基本代码的更多相关文章
- Spring框架的设计理念
它这种设计策略完全类似于Java实现OOP的设计理念,当然Java本身的设计要比Spring复杂太多太多,但是它们都是构建一个数据结构,然后根据这个数据结构设计它的生存环境,并让它在这个环境中按照一定 ...
- Spring框架概述
Spring是最流行的Java企业级应用开发框架,全球数以百万的开发者在使用Spring框架创建高性能.易测试.可重用的代码. Spring框架的核心特性可以应用于任何Java应用,但扩展的JavaE ...
- 初识Spring框架实现IOC和DI(依赖注入)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...
- Spring 框架的架包分析、功能作用、优点,及jar架包简介
Spring 框架的架包详解 Spring的作用 Spring的优势 由于刚搭建完一个MVC框架,决定分享一下我搭建过程中学习到的一些东西.我觉得不管你是个初级程序员还是高级程序员抑或 ...
- 最新 Eclipse IDE下的Spring框架配置及简单实例
前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...
- spring框架学习(三)
一.Spring自动组件扫描 Spring 提供组件扫描(component scanning)功能.它能从指定的classpath里自动扫描.侦测和实例化具有特定注解的组件. 基本的注解是@Comp ...
- Spring框架学习(一)
一. spring概述 Spring 框架是一个分层架构,由 7 个定义良好的模块组成.Spring 模块构建在核心容器之上,核心容器定义了创建.配置和管理 bean 的方式,如图 1 所示. 图 1 ...
- Spring 系列: Spring 框架简介 -7个部分
Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...
- 使用 Spring Boot 快速构建 Spring 框架应用--转
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...
随机推荐
- Linux下查/删/替 命令(转)
▪查看某目录下所有文件的个数: [root@localhost1 opt]# ls -l |grep "^-"|wc -l ▪查看某目录下所有文件的个数,包括子目录里面的: [ro ...
- vue2.0 axios交互
vue使用axios交互时候会出现的问题大致有三个: 1:本地调试跨域问题? 2:post请求,传参不成功,导致请求失败? 3:axios引用,在使用的组件里面引用 解决方案: 问题一:跨域? 解决本 ...
- 大数据时代的Python金融应用-Day1-Python与金融应用概述
一.Python语言的主要特征 1.开源性 Python和大多数的支撑库和工具都是开源的,通常可以非常灵活的使用而且有开放的协议. 2.解释性 也可以使用Cpython完成将解释性语言转化为实施可执行 ...
- oracle.exe 内存占用过大
现象: 明明各个schema 占用的磁盘空间都不大. oracle.exe 却占用了差不多 3G 的内存. 解决: 查了google,各种英文关键字没有找到原因. 最后,中文检索到了. https:/ ...
- [Spring] Aspect Oriented Programming with Spring | AOP | 切面 | 切点
使用Spring面向切面编程 1.介绍 AOP是OOP的补充,提供了另一种关于程序结构的思路. OOP的模块化的关键单位是 类 . AOP的则是aspect切面. AOP 将程序的逻辑分成独立的块(叫 ...
- USBCAN的使用和上位机开发(MFC)
USBCAN使用手册 参见:https://blog.51cto.com/12572800/2062839 1. USB CAN软件安装与硬件接线 USB CAN是常用的CAN测试工具.它的软件资料存 ...
- 20165303 预备作业3 Linux安装及学习
=20165303 预备作业3 Linux安装及学习 安装虚拟机 按照http://www.cnblogs.com/rocedu/p/6012545.html步骤进行逐步操作,安装虚拟机. 遇到的问题 ...
- JavaScript动态加载资源
//动态加载样式 function dynamicLoadingCss(path){ if(!path || path.length === 0){ return false; } var head ...
- 第二阶段——个人工作总结DAY07
1.昨天做了什么:昨天了解了一下时间抽也是一种ListView,然后就此学习了一下如何来修改. 2.今天打算做什么:今天在网上搜一些例子,找比较好看的界面,并实现代码. 3.遇到的困难:不知道最后连接 ...
- 函数指针做函数参数,其中有typedef的相关,感觉这是构成大河的小溪
#include<stdio.h> #include<stdlib.h> #include<string.h> int Funcadd(int a, int b) ...