Spring ioc学习

总体路经
在这里插入图片描述

User类 自己生成set,get方法

//简单类型
    private String id;
    private String name;
    private Date date;


    //    array,list,map,set,props(properties),自定义类型属于复杂类型
    private String[] strings;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties properties;

//    生命周期模拟
    public  void init(){
        System.out.println("创建User");
    }
    public void destroy(){
        System.out.println("销毁User");
    }

UserService

package com.lgp.service;

import com.lgp.bean.User;
import com.lgp.dao.UserDao;
//注解和注入包
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service//service注解
public class UserService {
    /*三种注入方式
    》1
    @Autowired
    或者
    @Autowired
    @Qualifier("bean的id")
    》2
    @Value("#{bean的id}")
    》3
    @Resource(name="bean的id值")*/

    @Autowired
//    @Value("#{userDao}")
//    @Resource(name="userDao")
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public boolean find(User user) {
        if(userDao.find(user)){
            return true;
        }else {
            return false;
        }
    }
}


UserDao

package com.lgp.dao;

import com.lgp.bean.User;
import org.springframework.stereotype.Repository;//dao注解包

@Repository//dao注解
public class UserDao {

    public boolean find(User user) {
        if("1".equals(user.getId())&&"czp".equals(user.getName())){
            return true;
        }else
            return false;
    }
}

MyClass

package com.lgp.bean;

public class MyClass {
//    单例模式
    private MyClass(){
        System.out.println("单例模式");
    }
    private static MyClass myClass;
    public static MyClass getInstance(){
        if(myClass == null){
            myClass = new MyClass();
        }
        return myClass;
    }
//    多实例
    public static MyClass getInstance2(){
        return new MyClass();
    }
}

UserFactory

package com.lgp.bean;
//模拟静态工厂
public class UserFactory {
    public static User getUser() {
        return new User();
    }
}

UserFactory2

package com.lgp.bean;
//模拟动态工厂
public class UserFactory2 {
    public User getUser() {
        return new User();
    }
}

LogUtils

package com.lgp.until;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogUtil {
    private static Logger logger;

    public static Logger getLogger(Object classes) {
        logger = LoggerFactory.getLogger((Class<?>) classes);
        return logger;
    }

}

Application.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--   context是注解配置
	当对象比较多的时候使用注解扫描
	只有标记有注解的类,才会被创建对象且添加到ioc容器中
	四个注解 
	@Component  //其他层,通用。当你不知道用那个时,可以用这个
	@Repository //Dao层
	@Service    //Service层
	@Controller("xxx")//Controller层
	注解完后,id默认为类名的首字母小写
	-->
<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
       ">
	
    <!--   spring创建date对象-->
    <bean id="birthday" class="java.util.Date"/>

   <!-- spring创建User对象并调用set方法赋值
    spring创建对象默认为单例:singleton。
    如何简单判断是否为单例模式,看内存地址是否相同
    可通过scope修改:prototype多例,singleton单例-->
    <bean id="user" class="com.lgp.bean.User" scope="singleton">
        <property name="id" value="1"/>
    <!--        ref引用date对象-->
        <property name="date" ref="birthday"/>
        <property name="name" value="czp"/>
    </bean>

    <bean id="user1" class="com.lgp.bean.User">
    <!--        constructor-arg有几个就表示调用有多少个参数的构造方法-->
        <constructor-arg name="id" value="2"/>
        <constructor-arg name="date" ref="birthday"/>
        <constructor-arg name="name" value="czp"/>
    </bean>

    <!--    静态工厂-->
    <bean class="com.lgp.bean.UserFactory" factory-method="getUser" id="getUser"/>

    <!--    实例工厂-->
    <bean class="com.lgp.bean.UserFactory2" id="factory2"/>
    <bean factory-bean="factory2" factory-method="getUser" id="getUser2"/>
    
	<!--生命周期模拟-->
    <bean id="userInitAndDestroy" class="com.lgp.bean.User" init-method="init" destroy-method="destroy"/>

	<!--    注入的本质是赋值-->
    <bean id="userShow" class="com.lgp.bean.User">
	<!--        property中 name是调用set方法,去掉set首字母小写-->
        <property name="strings">
            <array>
                <value>array1</value>
                <value>array2</value>
                <value>array3</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value>set1</value>
                <value>set1</value>
                <value>set2</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="110" value="map1"/>
                <entry key="111" value="map2"/>
                <entry key="112" value="map3"/>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="110">props1</prop>
                <prop key="111">props2</prop>
                <prop key="112">props3</prop>
            </props>
        </property>
    </bean>

    <bean id="userService" class="com.lgp.service.UserService">
        <property name="userDao" ref="userDao"/>
    </bean>
    <bean id="userDao" class="com.lgp.dao.UserDao"/>

	
    <!--以下为注解包扫描,当使用注解时,上面bean配置可以全部注释-->

	<!--    注解扫描com.lgp下的所有有注解的-->
    <context:component-scan base-package="com.lgp"/>
    
</beans>

test

package com.lgp;


import com.lgp.bean.MyClass;
import com.lgp.bean.User;
import com.lgp.service.UserService;
import com.lgp.until.LogUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppTest 
{
    Logger logger;
    ClassPathXmlApplicationContext context;
    @Before
    public void init(){
        context = new ClassPathXmlApplicationContext("ApplicationConfig.xml");
        logger = LogUtil.getLogger(AppTest.class);
    }
    @After
    public void destroy(){
        if(context != null)
            context.close();
    }
    @Test
    public void Test01()
    {
         User user = (User) context.getBean("user");
         User user2= (User) context.getBean("user");
//         User user1 = context.getBean("user1",User.class);
         logger.error(user.toString());
         logger.error(user2.toString());
    }
    @Test
    public void test02(){
//        静态工厂
        User user= context.getBean("getUser",User.class);
        logger.error(user.toString());
    }
    @Test
    public void test03(){
//        实例工厂
        User user= context.getBean("getUser2",User.class);
        logger.error(user.toString());
    }
    @Test
    public void test04(){
//        spring默认为单例
//        单例
        MyClass myClass1= MyClass.getInstance();
        MyClass myClass2= MyClass.getInstance();
//        地址相同
        logger.error(myClass1.toString());
        logger.error(myClass2.toString());

//        多实例
        MyClass myClass3= MyClass.getInstance2();
        MyClass myClass4= MyClass.getInstance2();
//        地址不同
        logger.error(myClass3.toString());
        logger.error(myClass4.toString());
    }
    @Test
    public void test05(){
//        生命周期
        User user = context.getBean("userInitAndDestroy",User.class);
        logger.error(user.toString());
    }
    @Test
    public void test06(){
//        注入
        User user = context.getBean("userShow",User.class);
        logger.error(user.toString());
    }
    @Test
    //使用注解
    public void test07(){
        UserService userService = context.getBean("userService",UserService.class);
        User user = context.getBean("user",User.class);
        boolean flag=userService.find(user);
        logger.error(flag+"");
    }
    
    //使用注解
    @Test
    public void test08(){
        UserService userService = context.getBean("userService",UserService.class);
        UserDao dao = context.getBean("userDao",UserDao.class);
        logger.error(userService+"userService");
        logger.error(dao+"userDao");
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值