Spring的"两个蝴蝶飞,你好"的简单开发(一)

东家蝴蝶西家飞,白骑少年今日归。 愿,所有迷茫的人,都不再迷茫的,愿,所有努力工作的人,都可以得到应有的回报,愿,所有的人,都可以找到回家的方向,愿,再无苦痛,再无离别。

上一章简单介绍了Hibernate的高级操作(十八),如果没有看过,请观看上一章

Spring 是一个非常牛逼的家族,老蝴蝶知识点太少,理论和实践均不足,所以,只是做一些简单的理解,不进行深入的分析和解释。 即,只讲常用的实战。(ps:接下来的文章均不做重复性的声明)

一. 为什么学习Spring

Spring 是一个开源的框架,是一个分层的一站式轻量级开源框架。 核心是 IOC (控制反转) 和AOP (面向切面)。

  1. 方便解耦,简化开发。
    将对象的创建和依赖关系的维护,交给Spring管理。
  2. AOP编程的支持
    可以方便的对程序进行权限拦截,运行监控,日志等功能
  3. 声明式事务的支持
    只需要通过简单的配置,就可以完成对数据库事务的管理,而不需要手动编程。
  4. 方便程序的测试
    对Junit4 的支持,可以通过注解方便的测试Spring 程序。
  5. 方便集合各种优秀的框架。
    内部提供了对Struts2 ,Hibernate,MyBatis Quartz的直接支持。
  6. 降低了 Java API的难度封装。
    对Java中的JDBC,JavaMail,远程调用等进行了封装,使API应用难度大大降低。
  7. Spring 的源码是学习的经典的范例,里面大量的设计模式和代码技巧。

二. Spring的下载

进行Spring 的官网,选择Spring FrameWork 板块进行下载,点击Learn ,就可以出现Spring 的各个版本链接,点击下载即可。
在这里插入图片描述

或者直接进入Spring 的版本历史下载地址:
http://repo.springsource.org/libs-release-local/org/springframework/spring

下载后的文件夹如下所示:
在这里插入图片描述
docs 为使用文档,里面放置了API 的帮助文档, libs 放置的是运行库, schema 放置了约束。 其中libs 放置的库是三个一组。
在这里插入图片描述
没有任何后缀的是运行包,-javadoc 是帮助文档, -sources 是源码。 在使用时,只需要把.jar 单独拿出来即可,引用一些常见的jar 包。
基础包有:
在这里插入图片描述

三. Spring以前开发的方式

三.一 创建一个简单的Person 类

在最开始的时候,是将对象的创建直接写入到程序中。

public Person getPerson(){
	Person p=new Person();
}

三.二 具有子类

Person 是一个抽象类,有两个具体的子类。 Student 和Teacher .
创建Student的时候

Public Person getStudent(){
		return new Student();
}

创建教师的时候

Public Person getTeacher(){
	return new Teacher();
}

后来,在工厂设计模式的时候,需要将一个标识传递进来,

public Person getPerson(String name){
		if("student".equals(name)){
			return new Student();
		}else if("teacher".equals(name)){
			return new Teacher();
		}
}

而这个标识,希望是动态的,即可以从配置文件,xml,properties ,或者是注解中读取出来,这样就可以动态的创建对象了。

三.三 工厂+反射+配置文件

//1. 在配置文件中,进行相应的配置, class 对应具体的全限定类名称
<bean id="student" class="com.yjl.pojo.Student"> 
//2 创建工厂
public BeanFactory{
		public Person getPerson(String id){
			Class class=Class.forName(id);
			return class.newInstance();
		}
}
//3 取得Bean
Person p=BeanFactory.getPerson("student");

传入的是一个student, 先找配置文件,找到了对应的全限定名称 Student, 这样知道创建的是一个Student 类,再利用反射机制,进行创建,返回Student 的对象。

三.四 面向接口编程

上面是面向类,具体的类进行的编号。 实际中,是利用面向接口进行相应的编号的。

PersonDao personD=new PersonDao();

后来是:

PersonDao personD=new StudentDaoImpl();

后来是:

public getPersonDao(String name){
	if("student".equals(name)){
		return new StudentDaoImpl();
	}
}

再后来:

//1. 在配置文件中,进行相应的配置, class 对应具体的全限定类名称
<bean id="studentDao" class="com.yjl.dao.StudentDaoImpl"> 
//2 创建工厂
public BeanFactory{
		public Person getPersonDao(String id){
			Class class=Class.forName(id);
			return class.newInstance();
		}
}
//3 取得Bean
PersonDao pd=BeanFactory.getPerson("studentDao");

后面会有具体的例子进行说明。

四 Spring的"两个蝴蝶飞,你好"的简单开发

四.一 创建POJO简单类

package com.yjl.pojo;
/**
 @author:yuejl
 @date: 2019年4月13日 下午3:29:48
 @Description spring的一个简单的类
*/
public class Person {
	/**
	 * @param name 名称
	 * @param description 描述
	 */
	private String name;
	private String description;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String say(){
		return this.name+":"+this.description;
	}
}

四.二 以前的实例化方式

@Test
	public void test1(){
		Person person=new Person();
		person.setName("两个蝴蝶飞");
		person.setDescription("一个充满希望的程序员");
		System.out.println(person.say());
	}

在这里插入图片描述

四.三 导入jar 包

导入基本的这些Spring 的jar包,添加到classpath 中。
在这里插入图片描述

四.四 创建spring的配置文件 applicationContext.xml

在src下创建spring的配置文件 appicationContext.xml, 名称一般命名为applicationContext.xml, 但并不是默认为applicationContext.xml 。 也可以用其他的文件名,如常见的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">
	<!-- id是不能重复的,且最好见名知意   class为全限定名称 -->
	<bean id="person" class="com.yjl.pojo.Person"></bean>
</beans>

约束,其他还有很多,这里只导入一个最需要的 beans.xml

四.五 Spring 方式的创建

@Test
	public void test2(){
		// 即使名称是applicationContext.xml, 也要填入相应的参数值。 而不是不传参数,默认找applicationContext.xml
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		// 具体类是如何使用的,后面解释。
		Person person=(Person) applicationContext.getBean("person");
		person.setName("两个蝴蝶飞");
		person.setDescription("一个充满希望的程序员");
		System.out.println(person.say());
	}

在这里插入图片描述
可以查看其构造方法

不传的话,不做处理。
在这里插入图片描述
传入一个值的话,内部处理成数组。
在这里插入图片描述
传入多个值,用的是可变参数接收,所以用"," 进行隔开
在这里插入图片描述
即:

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml","beans.xml");

而Hibernate 是采用的默认,其形式为:
在这里插入图片描述

五 Spring 面向接口编程

五.一 定义PersonDao的接口

package com.yjl.dao;

import com.yjl.pojo.Person;

/**
 @author:yuejl
 @date: 2019年4月13日 下午3:54:47
 @Description 类的相关描述
*/
public interface PersonDao {
	public Person getPersonByName(String name);
}

五.二 定义PersonDaoImpl实现

package com.yjl.dao;
import com.yjl.pojo.Person;
/**
 @author:yuejl
 @date: 2019年4月13日 下午3:56:33
 @Description 类的相关描述
*/
public class PersonDaoImpl implements PersonDao{

	@Override
	public Person getPersonByName(String name) {
		System.out.println("查询一个名称");
		if("两个蝴蝶飞".equals(name)){
			Person person=new Person();
			person.setName("两个蝴蝶飞");
			person.setDescription("一个充满希望的程序员");
			return person;
		}else{
			return null;
		}
	}
	
}

五.三 在applicationContext.xml中添加bean

<bean id="personDao" class="com.yjl.dao.PersonDaoImpl"></bean>

五.四 Spring创建接口

@Test
	public void test3(){
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonDao personD=(PersonDao) applicationContext.getBean("personDao");
		Person p=personD.getPersonByName("两个蝴蝶飞");
		System.out.println(p.say());
	}

运行生成:
在这里插入图片描述
将personDao 改成其他的名称,如personDao1

@Test
	public void test4(){
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonDao personD=(PersonDao) applicationContext.getBean("personDao1");
		Person p=personD.getPersonByName("两个蝴蝶飞");
		System.out.println(p.say());
	}

在这里插入图片描述
不能够创建PersonDaoImpl 对象,说未定义。

将查询的关键字改变,成"两个蝴蝶飞1"

@Test
	public void test5(){
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonDao personD=(PersonDao) applicationContext.getBean("personDao");
		Person p=personD.getPersonByName("两个蝴蝶飞1");
		System.out.println(p.say());
	}

在这里插入图片描述
在这里插入图片描述

六. log4j.properties 的引入

上面运行的过程中,一直有警告,说log4j 未引入。
从hibernate中找到log4j.properties,进行相应的引入,放置在src目录下,与applicationContext.xml 文件同级。
在这里插入图片描述

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
// 注意这一行,设置相应的级别
log4j.rootLogger=debug, stdout

再次运行,

不弹出warn ,改成info 了,那是因为log4j.properties 级别设置的是debug,默认的。
在这里插入图片描述
是运行的过程。
将级别改成info。
在这里插入图片描述
谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两个蝴蝶飞

你的鼓励,是老蝴蝶更努力写作的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值