zoukankan      html  css  js  c++  java
  • spring学习日志二

    一、spring依赖注入的方式

    1.通过set方法来完成注入

     <bean id="student" class="com.zhiyou100.xz.spring.Student" >
               <!-- property:通过set方法来注入Student类 的name属性值-->
               <property name="name" value="李四"/>
               <property name="age" value="18"/>
    </bean>

    2.通过构造方法来完成依赖注入

    <bean id="student2" class="com.zhiyou100.xz.spring.Student"> 
               <!-- 
                   constructor-arg:构造方法的参数
                   index:第几个参数 索引从0开始 
                       只有一个参数时,即当index="0"时value的值先默认的是string类型,要用name区分不同的参数名
                   name:通过构造方法的参数名
                -->
                <!-- 当一个构造方法有两个参数时,要用两个constructor-arg表示 -->
                <constructor-arg index="0" value="里斯"/>
                <constructor-arg index="1" value="18"/>
    </bean>

     测试

    public class Test {
         public static void main(String[] args) {
            //加载spring配置文件  
             ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
             //获取指定的类对象
             Student s=(Student) app.getBean("student2");
            //s.show();
             System.out.println(s.getName());
            System.out.println(s.getAge());
        }
    }

    二、依赖注入的数据类型

    1.基本数据类型和字符串使用value

    <bean id="address" class="com.zhiyou100.xz.spring.Address">
            <property name="address" value="南京"></property>
       </bean>

    2.如果是指向另一个对象的引用,使用ref

    //该类需要由spring容器来管理
    public class Student {
        private String name;//该类中属性的注入也由spring来注入
        private int age;
        private Address ad;//该属性是一个对象
    }
    <bean id="student" class="com.zhiyou100.xz.spring.Student" >
               <!-- property:通过set方法来注入Student类 的name属性值-->
               <property name="name" value="李四"/>
               <property name="age" value="18"/>
               <!-- ref指向另一个bean对象 -->
               <property name="ad" ref="address"/>
     </bean>  
     <bean id="address" class="com.zhiyou100.xz.spring.Address">
            <property name="address" value="南京"></property>
     </bean>

     3.如果类对象注入的属性类型为list类型

    //该类需要由spring容器来管理
    public class Student {
        private String name;//该类中属性的注入也由spring来注入
        private int age;
        private Address ad;//该属性是一个对象
        private List<Address> list;

      public List<Address> getList() {
        return list;
      }
      public void setList(List<Address> list) {
        this.list = list;
      }

    }

    <bean id="student" class="com.zhiyou100.xz.spring.Student" >
               <!-- property:通过set方法来注入Student类 的name属性值-->
               <property name="name" value="李四"/>
               <property name="age" value="18"/>
               <!-- ref指向另一个bean对象 -->
               <property name="ad" ref="address"/>
               <property name="list">
                   <list>
                       <bean class="com.zhiyou100.xz.spring.Address">
                        <property name="address" value="南京"></property>
                       </bean>
                       <bean class="com.zhiyou100.xz.spring.Address">
                        <property name="address" value="北京"></property>
                       </bean>
                       <bean class="com.zhiyou100.xz.spring.Address">
                        <property name="address" value="西京"></property>
                       </bean>
                   </list>
               </property>
    </bean>

    4.如果类对象注入的属性类型为map类型

    public class Student {
        private String name;//该类中属性的注入也由spring来注入
        private int age;
        private Address ad;//该属性是一个对象
        private List<Address> list;
        private Map<String, String> map;
        public Map<String, String> getMap() {
            return map;
        }
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
    }
    <bean id="student" class="com.zhiyou100.xz.spring.Student" scope="prototype">
               <!-- property:通过set方法来注入Student类 的name属性值-->
               <property name="name" value="李四"/>
               <property name="age" value="18"/>
               <property name="map">
                   <map>
                       <entry key="lisi" value="里斯"/>
                       <entry key="kd" value="恐蛋"/>
                       <entry key="dn" value="达尼"/>
                   </map>
               </property>
       </bean>

    三、Bean的作用域

    Bean的作用域默认为单例模式

     <!-- 
             scope:表示bean的作用域 默认为单例singleton
             prototype:原生。非单例      struts2:该框架要求非单例
      -->
       <bean id="student" class="com.zhiyou100.xz.spring.Student" scope="prototype">
               <!-- property:通过set方法来注入Student类 的name属性值-->
               <property name="name" value="李四"/>
               <property name="age" value="18"/>
               
       </bean>

    测试

    public class Test {
         public static void main(String[] args) {
            //加载spring配置文件  
             ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
             //获取指定的类对象
             Student s=(Student) app.getBean("student");
             Student s1=(Student) app.getBean("student");
             //s与s1的引用地址不同
        }
    }

    四、自动注入

    <?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.xsd"
        default-autowire="byName"
        >
         
         <bean id="userDao" class="com.zhiyou100.xz.spring.UserDao"></bean>
    <!--      <bean id="udao" class="com.zhiyou100.xz.spring.UserDao"></bean> -->
        <!-- autowire:自动注入的属性
             byType:根据userDao属性的类型,找到与之匹配的bean
             private UserDao userDao;
             byName:根据属性名找与之匹配的bean的id
             no:需要手动注入
             default:采取全局的default-autowire设置
         -->
        <bean id="uService" class="com.zhiyou100.xz.spring.UserService" autowire="default">
    <!--     <property name="userDao" ref="udao"></property> -->
    
        </bean>
        
     </beans>
     

    五、在spring配置文件中引入属性文件

    创建User.java

    public class User {
         private String name;
         private int age;
         private String address;
    }

    创建UserService.java

    public class UserService {
           private User user;
    public User getUser() { return user; } public void setUser(User user) { this.user = user; } }

    创建属性文件my.properties

    user.names=xz
    user.age=18
    user.address=u5357u4EAC

    创建app.xml配置文件

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 引入属性文件2.5以后就有了 
        如果引入多个文件可以使用通配符*,也可以使用逗号分割引入
        location="classpath:*.properties"
        location="classpath:my*.properties"
        location="classpath:my.properties,classpath:a.properties"
        -->
        <!-- 加载属性文件用context:property-placeholder  -->
        <context:property-placeholder location="classpath:my.properties"/>
        <bean id="users" class="com.zhiyou100.xz.spring.User">
              <property name="name" value="${user.names}"></property>
              <property name="age" value="${user.age}"></property>
              <property name="address" value="${user.address}"></property>
        </bean>
        
         <bean id="uService" class="com.zhiyou100.xz.spring.UserService" >
                <property name="user" ref="users"></property>
    
        </bean>
    </beans>

    测试

    public class Test2 {
         public static void main(String[] args) {
             ApplicationContext app=new ClassPathXmlApplicationContext("app.xml");
             UserService us=(UserService) app.getBean("uService");
             System.out.println(us.getUser()); 
             
         }
    }

    六、使用注解的方式

    1.引入jar包:

     2.配置文件中使用包扫描

    <?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:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd"
            >
        <!-- 1. 包扫描:扫描注解所在的包 component-scan:部件扫描-->    
        <context:component-scan base-package="com.zhiyou100.xz"></context:component-scan>
    
    </beans>

    3.在相应的类中加上注解

    @Repository   持久化注解。
    @Service 业务层注解
    @Controller 控制层注解
    @Autowired 自动注入 按照类型帮你自动注入,如果由多个类型相同的那么就会在按照名称注入。(建议使用这个)
    @Resouce  自动注入 按照名称注入,如果没有相同名称的bean那么会按照类型帮你注入。 它可以指定名称来注入。

    controller的代码

    @Controller(value="userController")
    public class UserController {
          
        @Autowired
        private UserService  userService;
    
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
        public String findById(int id) {
            userService.queryById(id);
            return "update";
        }
    }

    service的代码

    public interface UserService {
    
         public void queryById(int id);
       
    }
    @Service(value="userService")
    public class UserServiceImp implements UserService{
        //@Autowired  //注入了UserDao接口的实现类。按照类型注入<property name="userDao" ref="userDao"/>
        @Resource(name="userDao") //按照名称注入
        private UserDao userDao;//依赖注入UserDao接口的实现类对象   
        
        public void setUserDao(UserDao userDao) {  //set方法可省略
            this.userDao = userDao;
        }
    
    
        @Override
        public void queryById(int id) {
           userDao.selectById(id);   
              
        }
         
    }

    dao的代码

    public interface UserDao {
        public void selectById(int id);
    } 
    @Repository(value="userDao")
    public class UserDaoImp implements UserDao{
        
        @Override
        public void selectById(int id) {
            System.out.println("根据id查询数据库信息2");
        }
    }
  • 相关阅读:
    [No00009C]Visual Studio在 解决方案资源管理器 里同步定位打开的文件
    [No00009B]win10快捷键大全
    [No000099]软件版本命名规范
    [No000098]SVN学习笔记5-分支,合并,属性,补丁,锁,分支图
    [No000097]程序员面试题集【下】
    [No000096]程序员面试题集【上】
    [No000095].Net入门级逆向工程-1.SpreadsheetGear汉化
    [No000093]按住Alt 再按数字键敲出任意汉字和字符!
    [No000092]SVN学习笔记3-Import/Checkout(迁入/迁出),GetLock(加锁)
    ubuntu 查看端口被占用并处理
  • 原文地址:https://www.cnblogs.com/sitian2050/p/11474745.html
Copyright © 2011-2022 走看看