logo头像

一路过来,不过游牧自己。。

SSM之Spring(一)


写了一晚上的Spring,结果一下子没保存,一下子全删了,还以为是Hexo除了问题,找了半天bug(痛苦脸),最后还是重写吧!
最近一直在学习Spring,下面写写关于Spring的一些东西!

一、Spring简介

Spring是一个轻量级的框架,所谓的框架就是给你提供一种规则!
1、Spring:春天,意味着Spring的出现给软件行业带来了春天
2、理念:使现有技术更加实用,本身也是一种大杂烩,整合现在的技术
3、Spring优点:
轻量级框架
ioc容器———控制反转(重点)

二、初识Spring

关于Spring,我是从一个例子入手的,思路如下:
1、导入相关的Spring的jar包(注意要导入commons—logging包)
2、写一个类Hello,内有name属性,有setname()方法,show()函数等
3、写一个配置文件,注意要加入头文件
4、写一个Test类来测试

那我们直接从代码入手来直观的去感受一下关于Spring的东西!

1、导入Spring的jar包

在项目中导入包如下

1
2
3
4
5
6
7
8
9
10
11
12
13
commons-logging-1.1.1.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar

如上,记得到最后要BuildPath一下!

2、写一个Hello类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Hello {

public Hello() {
System.out.println("Hello 被创建!");
}
private String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public void show(){
System.out.println("hello"+name);

}
}

Hello类中要定义的东西只有一个name属性,然后去设置这个属性的方法还有一个Show()方法

3、写配置文件

首先要在配置文件中加入头文件:

1
2
3
4
5
6
<?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">
</beans>

然后对bean文件进行书写

1
2
3
4
5
<!-- bean就是java对象,由Spring来创建和管理 -->
<bean name="hello" class="cn.bean.Hello">
<!-- 这里注意name是函数中的set函数所持有的名字 -->
<property name='name' value="youngerfary"></property>
</bean>

这里都有注释,不在赘述

4、写一个Test类

写的Test类如下:

1
2
3
4
5
6
7
8
9
public class Test {
public static void main(String[] args) {
//先要加载这个类
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//这里的Bean是配置文件中的id,也是一个name
Hello hello=(Hello)ac.getBean("hello");
hello.show();
}
}

在show之后,控制台的运行结果如下所示:

1
2
Hello 被创建!
helloyoungerfary

这就大功告成了!

三、思考

当然这不仅仅是一个例子,我们从例子中来得到什么?
1、hello对象是由谁创建的?
由Test类看来,程序并没有创建一个对象,这个对象是由Spring容器创建的
2、Hello属性是怎样设置的?
由Spring容器来设置,这个容器初步走在xml文件中!

这个过程我们称之为控制反转!!
控制的内容:指谁来控制对象的创建!传统的应用程序对象的创建由程序本身控制的,使用Spring之后来创建。

关于正转和反转:
正转:指由程序来创建对象
反转:以前对象是由程序本身来创建,使用Spring之后,程序变为被动接受Spring创建的对象
也可以称作为依赖注入!!

四、实战例子二

为了更好理解这种思想,我决定再写一个小例子!

1、首先我们先定义一个Dao接口:

1
2
3
4
5
public interface UserDao {

public void getuser();

}

2、两个不同的实现:

一个是Mysql

1
2
3
4
5
public class UserDaoMysqlimpl implements UserDao {
public void getuser(){
System.out.println("Mysql获取用户数据");
}
}

一个是Oracle

1
2
3
4
5
6
public class UserDaoOracleimpl implements UserDao{
public void getuser(){
System.out.println("Oracle获取用户数据");
}

}

3、再写一个Service接口:

1
2
3
public interface UserService {
public void getuser();
}

4、service接口的实现:

1
2
3
4
5
6
7
8
9
public class UserServiceimpl implements UserService {
private UserDao userDao=null;
public void setUserDao(UserDao userdao){
this.userDao=userdao;
}
public void getuser(){
userDao.getuser();
}
}

5、写一个配置文件,也包含了头文件

1
2
3
4
5
6
7
8
9
10
11
12
<?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="mysqlDao" class="cn.dao.impl.UserDaoMysqlimpl"/>
<bean id="oracleDao" class="cn.dao.impl.UserDaoOracleimpl"/>
<bean id="service" class="cn.service.impl.UserServiceimpl">
<!-- ref引用对象(对象是由spring来创建的) -->
<property name="userDao" ref="oracleDao"></property>
</bean>
</beans>

6、Test类的编写

1
2
3
4
5
6
7
8
 public class Test {
public static void main(String[] args) {

ApplicationContext acApplicationContext=new ClassPathXmlApplicationContext("beans.xml");
UserService us = (UserService)acApplicationContext.getBean("service");
us.getuser();
}
}

通过以上例子编写,这个程序的最终结果是什么呢?

1
Oracle获取用户数据

很nice有没有!!我们这时候创建对象不用再去改代码了,只要改配文件就OK了!

学习之路漫漫长,多走路,多思考!
——————YoungerFary

微信打赏

赞赏是不耍流氓的鼓励