spring初体验

一。引入jar包
spring.jar    commons-loggion-1.0.4.jar

二。springIOC,是把一个对象注入到另一个对象。
   处理接口
package com.job.dao;

import com.job.model.User;

public interface UserDAO {
	public void save(User u);
}


  接口实现
package com.job.dao;

import com.job.model.User;

public class UserDAOImpl implements UserDAO{

	@Override
	public void save(User u) {
		System.out.println(u.getUsername()+"---haha!");
	}
}


  服务层
public class UserService {
	
	private UserDAO userDAO;
	
	public void add(User u){
		this.userDAO.save(u);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
}


  实体类
public class User {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}


  spring的application.xml配置(使用set方法注入)
<?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:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<bean id="userService" class="com.job.service.UserService" lazy-init="true">
		<!-- 使用setter方法注入对象属性值 -->
		<property name="userDAO" ref="userDAOImpl"></property>
	</bean>

	<bean id="userDAOImpl" class="com.job.dao.UserDAOImpl" lazy-init="true">
	</bean>

</beans>


  测试
public class UserServiceTest {
	@Test
	public void test(){
		ApplicationContext ac =  new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService service = (UserService)ac.getBean("userService");
		User u = new User();
		u.setUsername("me haha  ");
		service.add(u);
		
	}
}

ClassPathXmlApplicationContext方法不写路径,则默认读取项目根路径src下applicationContext.xml文件

测试结果
引用

me haha  ---haha!


-----------------------------------------------------------------
使用构造方法注入属性值
一。在服务层加入构造方法
public class UserService {
	
	private UserDAO userDAO;
	
	public UserService(UserDAO userDAO) {
		super();
		this.userDAO = userDAO;
	}

	public void add(User u){
		this.userDAO.save(u);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
}



二。配置applicationContext.xml
	<bean id="userService" class="com.job.service.UserService" lazy-init="true">
		<!-- 使用构造方法注入对象属性值 -->
		<constructor-arg  ref="userDAOImpl"></constructor-arg>
	</bean>

	<bean id="userDAOImpl" class="com.job.dao.UserDAOImpl" lazy-init="true">
	</bean>


<bean id="userService" class="com.job.service.UserService" lazy-init="true" scope="prototype">
scope不加,默认为singleton单例。prototype表示以第一个为原型创建一个

<constructor-arg  ref="userDAOImpl" index="0" type="int" value="11" ></constructor-arg>

index表示第几个参数,type表示参数类型,value表示如果是基本类型则转化成对应类型

你可能感兴趣的:(spring)