IOC容器,依赖注入

3 篇文章 0 订阅
2 篇文章 0 订阅

1.Ioc容器
   (1)读取配置信息(xml文件、配置类、注解配置)  ClassPathXmlApplicationContext()            annoationConfigApplicationContext()
   (2)根据bean的查找实体类   ---getBean("")
   (3)将实体类的class文件装载到spring容器 ---new 实体类()
   (4)使用bean --调用属性或方法或者其他的需求
   
2.beanFactory接口和applicationContext接口
      beanFactory :bean工厂,是ioc的核心,spring底层基础建设,采用延迟加载
      applicationContext:应用上下文,是由BeanFactory派生而来,提供了更多面向实际应用的功能。
                     面向spring使用和开发的,两个实现类
                       ClassPathXmlApplicationContext()从class文件中加载配置文件
                       FileSystemApplicationContext()从文件系统加载配置文件
3.依赖注入DI
    (1)配置类java @configration  @bean   (实际开发中比较少用)
    (2)xml方式注入(推荐使用 --属性注入更多)
          a.构造方法注入
          b.属性注入 /set注入
          c.接口注入/(spring未实现 )
    (3)注解方式(推荐)
    
    a.构造方法注入 
         //实体类
        

 public class UserServiceImpl implements UserService{
                private UserDao userDao;
                private String str;
                private List<String> list;
                private Set<User> set;
                private Map<String,User> map;

                public boolean login(String userName, String password) {
                    System.out.println("----------UserServiceImpl----------");
                    System.out.println(str);
                    System.out.println(list);
                    System.out.println(set);
                    System.out.println(map);
                    return this.userDao.login(userName, password);
                }

                public UserServiceImpl(UserDao userDaol,String str,List<String> list,Set<User> set,Map<String,User> map) {
                    super();
                    this.userDao = userDaol;
                    this.str=str;
                    this.list=list;
                    this.set=set;
                    this.map=map;
                }
            }


        //配置文件
        

     <?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:p="http://www.springframework.org/schema/p"
                    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

                  <bean name="userSeriver" class="com.neuedu.service.UserServiceImpl">
                   <constructor-arg name="userDao" ref="userDao"></constructor-arg>
                   <constructor-arg name="str" value="张三"></constructor-arg>
                   <constructor-arg name="list">
                        <list>
                           <value>qqqq</value>
                            <value>www</value>
                             <value>eeee</value> 
                        </list>
                   </constructor-arg>
                   <constructor-arg name="set">
                       <set>
                             <ref bean="u1"/>如果是别的类,必须参照其他的类
                             <ref bean="u2"/>
                             <ref bean="u3"/>
                       </set>
                   </constructor-arg>
                   <constructor-arg name="map">
                      <map>
                         <entry key="zhangsan" value-ref="u1"></entry>
                         <entry key="wewe" value-ref="u2"></entry>
                         <entry>
                                <key><value>xcxcx</value></key>
                                <ref bean="u1"></ref>
                         </entry>
                      </map>
                   </constructor-arg>
                </bean>
                <bean name="userDao" class="com.neuedu.dao.UserDaoImpl"></bean>
                <bean name="u1" class="com.neuedu.entity.User">
                   <property name="username" value="战三"></property>
                   <property name="password" value="122435"></property>
                   <property name="age" value="12"></property>
                </bean>
                <bean name="u2" class="com.neuedu.entity.User"></bean>
                <bean name="u3" class="com.neuedu.entity.User"></bean>
                </beans>


    //测试类(和xml文件在同一个包下)
  

     @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration(locations={"appConfig.xml"})
        public class AllTests {
            @Autowired
            UserService   userSeriver;
           @Test
           public void tt(){
               userSeriver.login("sss", "2324"); 
           }
        }
        


   b.属性注入 /set注入
       要求:实体类必须有set方法
    //实体类
      

  public class UserServiceImpl implements UserService{
            private UserDao userDao;
            private String str;
            private List<String> list;
            private Set<User> set;
            private Map<String,User> map;

            public boolean login(String userName, String password) {
                System.out.println("----------UserServiceImpl----------");
                System.out.println(str);
                System.out.println(list);
                System.out.println(set);
                System.out.println(map);
                return this.userDao.login(userName, password);
            }
           //setters 
            public void setUserDao(UserDao userDao) {
                this.userDao = userDao;
            }
            public void setStr(String str) {
                this.str = str;
            }
            public void setList(List<String> list) {
                this.list = list;
            }
            public void setSet(Set<User> set) {
                this.set = set;
            }
            public void setMap(Map<String, User> map) {
                this.map = map;
            }
        }    


     //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:p="http://www.springframework.org/schema/p"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
         <bean name="uservice" class="com.neuedu.service.UserServiceImpl">
            <property name="userDao" ref="uDao"></property>
            <property name="str" value='1223'></property>
            <property name="list">
                <list>
                   <value>dfdf</value>
                   <value>dfdf</value>
                   <value>dfdf</value>
                </list>
            </property>
            <property name="set">
                <set>
                    <ref bean="u1"></ref>
                    <ref bean="u2"></ref>
                </set>
            </property>
            
            <property name="map">
                <map>
                   <entry key="11" value-ref="u1"></entry>
                   <entry key="22" value-ref="u2"></entry>
                </map>
            
            </property>
            
         </bean>

        <bean name="uDao" class="com.neuedu.dao.UserDaoImpl"></bean>
        <bean name="u1" class="com.neuedu.entity.User">
           <property name="username" value="ddd"></property>
           <property name="age" value="18"></property>
        </bean>
        <bean name="u2" class="com.neuedu.entity.User">
         <property name="username" value="ccc"></property>
           <property name="age" value="23"></property>
        </bean>
        </beans>

    c.基于注解注入
      
    //appannotConfig.xml文件
       注解扫描器 
          <!-- 加载扫描器 -->
           <context:component-scan base-package="com.neuedu"/>
    //实体类
    
       //注册组件
      

  @Component("uservice")
        public class UserServiceImpl3 implements UserService{
            @Autowired  //自动装配按照类型查找  (接口类型)
            @Qualifier("dao2")//将装配改成按名字查找
            private UserDao userDao;
            public boolean login(String userName, String password) {
                System.out.println("----------UserServiceImpl----------");
                return this.userDao.login(userName, password);
            }

            
        }


    //dao层
 

   @Component("dao2")
        public class UserDaoImpl2 implements UserDao {
            public boolean login(String userName, String password){
                System.out.println("----------UserDaoImpl222222222222222----------");
                System.out.println(userName + " login.......");
                return true;
            }
        }


    //测试类
 

   @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration(locations={"appannotConfig.xml"})
        public class AllTests3 {
            @Autowired
            UserService   uservice;
           @Test
           public void tt(){
               uservice.login("sss", "2324");
               
           }
        }
        


    注解详细信息
        @component定义组件 ----类上
        @autowired 自动装配bean---属性(按照接口类型查找)
        @qualifier 按照名字查找bean,通常和@autowired(使用场景:接口有多个实现类时,用于指定某一个类的)
        
        Spring还提供了三个功能基本和@Component等效的注解,
        分别用于对DAO,Service和Web层的Controller进行注解
        ①@Repository:用于对DAO实现类进行标注 
        ②@Service:用于对Service类进行标注 
        ③@Controller:用于对Controller实现类进行标注
        
        
    @resource注解  等效于 @Autowired+@qualifier   ---用于修饰属性
      
       /*@Autowired  //自动装配按照类型查找  (接口类型)
    @Qualifier("userDaoImpl")//将装配改成按名字查找
    */    等效于
    @Resource(name="userDaoImpl")
    
4,  bean作用域 scope的取值
     单例  singleton
     多实例  prototype
     请求request
     会话 session
     应用 globalsession
       xml文件:
       <bean name="userSeriver" class="com.neuedu.service.UserServiceImpl1" scope="prototype">
           <constructor-arg name="userDaol" ref="userDao"></constructor-arg>
       </bean>
       
      

   public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("appConfig.xml");
        UserService u1=(UserService)app.getBean("userSeriver");
        UserService u2=(UserService)app.getBean("userSeriver");
        System.out.println(u1+"ccc"+u2);
        System.out.println(u1==u2);  //false

    }


     
     一般我们对有状态的bean使用prototype作用域,而对无状态的bean使用singleton作用域。
     在Spring环境下对于所有的DAO类都可以采用单例模式,因为Spring利用AOP和LocalThread功能,
     对非线程安全的变量(或称状态)进行处理,使这些非线程安全的类变成了线程安全的类。
     
     
     有状态的bean:定义了成员属性的类
     无状态的bean:没有定义成员属性的类
    
    ---总结:spring是线程安全的:
           1.有状态的bean可以用prototype多实例模式
           2.无状态的bean采用本地线程LocalThread功能实现的线程安全。
        

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值