spring 配置
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>
<property name="参数名" value="参数值" ref="关联某个bean的名称"></property>
</bean>
</beans>
3、获得bean工厂
Resource r = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(r);
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();
}
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……");
}
}
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>
<!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 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 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>
</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;
}
}
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();
}
}
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();
}
}