spring是当前流行的一个框架。在学习spring之前当然的先准备一些jar包。可以在官网找找,(也可以留言,网盘分享);
基于IDEA环境学习。
第一天完成的任务:
1.使用spring框架输出一句话;
2.熟悉spring的注入。
3.使用配置的方式在spring中连接数据库。
代码1:
1 package test;
2
3 /**
4 * Created by Administrator on 2016/12/4.
5 * 1.用spring框架输出一句话
6 */
7 public class Demo1 {
8 public Demo1(){
9 System.out.println("我的第一个spring程序");
10 }
11 public void init(){
12 System.out.println("这是init方法");
13 }
14 public void destory(){
15 System.out.println("调用cx.close方法之后");
16 System.out.println("这是destory方法");
17 }
18 }
代码2:
package tools; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; /**
* Created by Administrator on 2016/12/4.
*/
public class JdbcUtils {
private static String url;
private static String user;
private static String pwd;
private static String driverClass;
public static Connection getconn(){
Connection conn=null;
try {
conn= DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
} public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getDriverClass() {
return driverClass;
}
public void setDriverClass(String driverClass) {
try {
Class.forName(driverClass);
this.driverClass = driverClass;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public String toString() {
return "Demo2 \n[url=" + url + "]\n[user=" + user + "]\n[pwd=" + pwd
+ "]\n[driverClass=" + driverClass + "]";
}
public JdbcUtils(String url, String user, String pwd, String driverClass) {
super();
this.url = url;
this.user = user;
this.pwd = pwd;
this.driverClass = driverClass;
}
public JdbcUtils() {
super();
// TODO Auto-generated constructor stub
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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:property-placeholder location="classpath:jdbc.properties"/>
<bean id="myJdbcUtils" class="tools.JdbcUtils"
p:url="${url}"
p:user="${user}"
p:pwd="${password}"
p:driverClass="${driver}"
/>
<bean id="Demo1" class="test.Demo1">
</bean>
<bean id="Demo2" class="test.Demo1" init-method="init" destroy-method="destory">
</bean>
</beans>
测试代码:
package test; import org.springframework.context.support.ClassPathXmlApplicationContext;
import tools.JdbcUtils; /**
* Created by Administrator on 2016/12/4.
*/
public class JdbcUtilsTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext cx=new ClassPathXmlApplicationContext("demo1.xml");
JdbcUtils myJdbcUtils=(JdbcUtils) cx.getBean("myJdbcUtils");
System.out.println(myJdbcUtils);
}
}