码迷,mamicode.com
首页 > 编程语言 > 详细

spring 配置

时间:2015-10-22 12:07:08      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

1、导入必备jar包   spring.jar  和 Commons-logging.jar文件
2、 声明Javabean、创建beans.xml配置文件    xml(Basic Templates)-->选择spring DTD文档 可以自动生成说明
<beans>
        <bean id="名称" class="类的位置">
           <property name="参数名" value="参数值"  ref="关联某个bean的名称"></property>
      </bean>
</beans>
3、获得bean工厂 
Resource r = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(r);
4、获得对象
           UserDAO dao = (UserDAO)factory.getBean("userDao");
5、测试
 
例子:
首先创建ConnectionUtil 链接的javabean
package connTest;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionUtil {
private String username;
private String password;
private String url;
private String driver;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUrl(String url) {
this.url = url;
}
public void setDriver(String driver) {
this.driver = driver;
}
public Connection getConnection() {
try {
Class.forName(driver);
return DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
userDAO文件
package connTest;

public interface UserDAO {
public void register();
}
UserDaoImpl 文件
package connTest;

import java.sql.Connection;

public class UserDaoImpl implements UserDAO{
//依赖于数据库连接操作
private ConnectionUtil util;

public void setUtil(ConnectionUtil util) {
this.util = util;
}

public void register() {
Connection conn = util.getConnection();
System.out.println(conn);
System.out.println("register……ok……");
}

}
beans.xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
<beans>
   <bean id="conn" class="connTest.ConnectionUtil">
   <property name="username" value="sa"></property>
   <property name="password" value="dou123"></property>
   <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver">                  
   </property>
    <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=mydb">        
    </property>
</bean>
<bean id="userDao" class="connTest.UserDaoImpl">
<property name="util" ref="conn"></property>
</bean>
User类javabean
package connTest;

public class User {
private String username;
private String passwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
验证、实现Login 类
package connTest;

import java.sql.Connection;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Login {

public static void main(String[] args) {
Resource r = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(r);
UserDAO dao = (UserDAO)factory.getBean("userDao");
dao.register();
}

}

spring 配置

标签:

原文地址:http://www.cnblogs.com/doudingbest/p/4900319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!