了解Spring set依赖注入

  1. 新建一个java项目,不是动态Web项目;就是一个简单的java项目;

  2. 创建domain模型 User.java

    public class User {
        private int id;
        private String username;
        private String password;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        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;
        }
    }
  3. 创建lib文件夹,导入下列包,全选右键->”Build Path”->”add …”

    了解Spring set依赖注入_第1张图片

  4. 创建上下文环境配置文件ApplicationContext.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="userManager" class="cn.com.xalead.spring.UserManager">
            <property name="driverCalss">
                <value>jdbc.msyql.Drivervalue>
            property>
            <property name="username">
                <value>rootvalue>
            property>
            <property name="password">
                <value>rootvalue>
            property>
            <property name="url">
                <value>jdbc:mysql://localhost:3306value>
            property>
        bean>
    
        
        <bean id="userAction" class="cn.com.xalead.spring.UserAction">
            <property name="manager" ref="userManager" />
        bean>
    
    beans>
  5. 创建一个UserManager类,模拟对User对象的操作;
    变量username, password, url, driveclass是模仿jdbc连接(没有具体实现连接,只是利用变量实现依赖注入的赋值操作);

    public class UserManager {
        private String username;
        private String password;
        private String url;
        private String driverCalss;
    
        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 setDriverCalss(String driverCalss) {
            this.driverCalss = driverCalss;
        }
    
        public void add(User user){
            System.out.println(username);
            System.out.println("执行添加User对象的操作!");
        }
    }
  6. 创建一个UserAction类,类似MVC三层架构中的controller

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.BeanFactoryAware;
    
    public class UserAction{
        private UserManager manager = null;
        public void setManager(UserManager manager) {
            this.manager = manager;
        }
    
        public String execute(){
            User user = new User();
            manager.add(user);
            return "success";
        }
    }
  7. 测试

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainClass {
    
        public static void main(String[] args) {
            BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    //      BeanFactory factory = new ClassPathXmlApplicationContext("app*.xml");
    
            UserAction ua = (UserAction)factory.getBean("userAction");
            System.out.println(ua.execute());
    
        }
    
    }

    测试结果:

你可能感兴趣的:(•,Java,spring)