Spring第一天

package com.fxr.spring.model;

public class User {

private int id;
private String name;

public User(int id,String name){
this.id = id;
this.name = name;
}

public User(){

}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

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

@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}




}

===================================================================

package com.fxr.spring.dao;

import com.fxr.spring.model.User;

public interface IUserDao {

public void add(User u);

public void delete(int id);


}

=============================================

package com.fxr.spring.dao;

import com.fxr.spring.model.User;

public class UserDao implements IUserDao{

@Override
public void add(User u) {

System.out.println("添加"+u);

}

@Override
public void delete(int id) {
System.out.println("删除"+id);

}

}

 

=====================================

package com.fxr.spring.service;

import com.fxr.spring.dao.UserDao;
import com.fxr.spring.model.User;

public interface IUserService {

public void add(User u);
public void delete(int id);




}

 

===============================

package com.fxr.spring.service;

import com.fxr.spring.dao.UserDao;
import com.fxr.spring.model.User;

public class UserService implements IUserService{

private UserDao userdao;




public UserDao getUserdao() {
return userdao;
}

public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}

@Override
public void add(User u) {
userdao.add(u);

}

@Override
public void delete(int id) {
userdao.delete(id);

}

}

 

=============================

package com.fxr.spring.action;

import com.fxr.spring.model.User;
import com.fxr.spring.service.UserService;

public class UserAction {

private User u;
private int id;
private UserService userservice;

public UserAction(){

}




public User getU() {
return u;
}

public void setU(User u) {
this.u = u;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public UserService getUserservice() {
return userservice;
}

public void setUserservice(UserService userservice) {
this.userservice = userservice;
}

public void add(){
userservice.add(u);

}

public void delete(){
userservice.delete(id);
}




}

































1、导入spring的dist中的jar包和commons-logging包 2、在src目录下创建相应的beans.xml 3、为beans.xml添加相应的schema <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-3.0.xsd"> </beans> 4、新建一个类 5、在beans.xml中创建对象 <!-- 创建如下bean等于完成了:HelloWorld helloWorld = new HelloWorld --> <bean id="helloWorld" class="org.zttc.itat.spring.model.HelloWorld"/> 6、在测试类中使用这个对象 6.1、创建Spring的工厂 private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml"); 6.2、通过Spring工厂获取相应的对象 //此处getBean中的helloWorld就是beans.xml配置文件中的id HelloWorld hello = factory.getBean("helloWorld",HelloWorld.class); //此时的hello对象就是被Spring说管理的对象 System.out.println(hello.hello()); ================================================= bean.xml <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-3.0.xsd"> <bean id="helloworld" class="com.fxr.spring.model.HelloWorld"></bean> </beans> ================================================= 测试: package com.fxr.spring.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.fxr.spring.model.HelloWorld; public class Test { private static BeanFactory factory = new ClassPathXmlApplicationContext("bean.xml"); public static void main(String[] args) { //创建一个类 HelloWorld hello = (HelloWorld) factory.getBean("helloworld"); hello.say(); } }

  //如果在bean.xml中没有配置scope,默认是单例的模式,当把scope配置成protex的时候,就是多例的模式

<?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-3.0.xsd">

<!--
创建如下bean等于完成了:HelloWorld helloWorld = new HelloWorld()
-->
<bean id="helloWorld" class="类的路径" scope="prototype"/>

<!-- 创建了一个User对象user,id为1,username为悟空,如果要注入值不使用ref而是使用value -->
<bean id="user" class="类的路径">
<property name="id" value="2"/>
<property name="username" value="八戒"/>
</bean>

<bean id="userDao" class="类的路径"></bean>
<bean id="userJDBCDao" class="类的路径"></bean>

<!-- autowire=byName表示会根据name来完成注入,
byType表示根据类型注入 ,使用byType注入如果一个类中有两个同类型的对象就会抛出异常
所以在开发中一般都是使用byName
虽然自动注入可以减少配置,但是通过bean文件无法很好了解整个类的结果,所以不建议使用autowire-->
<bean id="userService" class="类的路径">
<!-- name中的值会在userService对象中调用setXX方法来注入,诸如:name="userDao"
在具体注入时会调用setUserDao(IUserDao userDao)来完成注入
ref="userDao"表示是配置文件中的bean中所创建的DAO的id -->
<property name="userDao" ref="userDao"></property>
</bean>

<!-- 对于UserAction而言,里面的属性的值的状态会根据不同的线程得到不同的值,所以应该使用多例 -->
<bean id="userAction" class="类的路径" scope="prototype">
<property name="userService" ref="userService"/>
<property name="user" ref="user"/>
<property name="id" value="12"/>
<!-- 同样可以注入列表,但是也不常用 -->
<property name="names">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
</bean>

<!-- 以下是使用构造函数来注入,不常用,基本都是使用set方法注入 -->
<!-- <bean id="userAction" class="类的路径" scope="prototype">
<constructor-arg ref="userService"/>
</bean> -->
</beans>

 

1、将所有的类在beans.xml中创建
2、对有依赖的类完成注入
2.1、为每一个依赖类创建相应的getter和setter方法

posted on 2015-02-04 15:14  aicpcode  阅读(179)  评论(0编辑  收藏  举报

导航