Spring笔记

Spring 学习

Spring 以Ioc(控制反转)和AOP(面向切面编程)为内核的开源框架。I

何为IoC?

控制反转或者依赖注入,通常创建对象时是new xxx对象,
而“反转”就是对象的调用不再由调用者调用,而由spring容器创建。控制权发生变化。就相当于一个中介。
为啥多此一举的弄多个第三方吗?降低大型系统的耦合性。

一位同志写的
IOC
耦合性

核心容器

BeanFactory 和ApplicationContext
BeanFactory 不常用或者资源少时使用,是Spring里面最低层的接口,提供了最简单的容器的功能,
只提供了实例化对象和拿对象的功能;

BeanFactory在启动的时候不会去实例化Bean,中有从容器中拿Bean的时候才会去实例化;
ApplicationContext在启动的时候就把所有的Bean全部实例化了。它还可以为Bean配置lazy-init=true来让Bean延迟实例化;
BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("路径"));
ApplicationContext
BeanFactory的子接口多了AOP 事务 国际化等方面支持
两种创建方式

ClassPathXmlApplicationContext从类路径下加载 xmL 配置文件

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService)applicationContext.getBean("bookService");

FileSystemXmlApplicationContext不常用,从文件系统中加载配置文件

ApplicationContext applicationContext1 = new FileSystemXmlApplicationContext("xx.xml");

bean实例化

3种 

无参构造器 根据默认无参构造方法来创建类对象

public class bean {
    public void test(){
        System.out.println("无参构造实例化");
    }
}
<bean id="bean1" class="dao.bean"></bean>

静态工厂

 <bean id="bean2" class="dao.staticFactory" factory-method="createBean"></bean>
public class staticFactory {
    public static bean createBean(){
        return new bean();
    }
}

实例工厂

<bean id="beanfactory" class="dao.instanceFactory"></bean>
    <bean id="bean3" factory-bean="beanfactory" factory-method="createBean"></bean>
public class instanceFactory {
    public  bean createBean(){
        return new bean();
    }
}

区别:。。。。

bean的装配

bean的装配(依赖注入)与实例化的区别:实例化只是new一个对象而转配过程是先实例化对象再注入属性或者其他依赖对象。
4种装配方式:XML ,ANNOTATION(注解),自动装配。

XML装配:

1.构造配置

private int id;
    private String name;
    private List<String> list;
    //构造配置
    public User(int id, String name, List<String> list) {
        this.id = id;
        this.name = name;
        this.list = list;
    }
<bean id="user1" class="User">
    <constructor-arg index="0" value="1"></constructor-arg>
    <constructor-arg index="1" value="yu"></constructor-arg>
    <constructor-arg index="2">
        <list>
            <value>"ddd"</value>
            <value>"cccc"</value>
        </list>
    </constructor-arg>
</bean>

2.setter必须有无参构造
相对应的set方法

//setter配置
    public User() {
    }
    public void setId(int id) {
        this.id = id;
    }
	 public void setName(String name) {
        this.name = name;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
  <bean id="user2" class="User">
        <property name="name" value="z"></property>
        <property name="id" value="1"></property>
        <property name="list">
            <list>
                <value>"ddd"</value>
                <value>"cccc"</value>
            </list>
        </property>
     </bean>

注解装配

要求命名空间context,
扫描context:component-scan base-package=""

@Component取代bean class=""
@Component("id")取代bean id="" class="“
另外几个相同果,名字不一样,用在不同地方区别
DAO层:@Repository
service层:@Service
web层:@Controller
<context:component-scan base-package="users"></context:component-scan>
@Repository("bean4")
public class User {
    private int id;
    private String name;
    public User() {

    }
}
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 User user = (User) applicationContext.getBean("bean4");
 System.out.println(user);

注解的依赖注入

@value("xx")给私有字段设置,也可以给setter方法设置
@Autowired 按照类型引入
@Autowired
@Qualifier(“名称”) 按照名称

=@Resource(name=“名称”)
@PostConstruct 初始化
@PreDestroy 销毁
、、、、
@Component
public class xx {
}
@Repository("bean4")
public class User {
    @Value("1")
    private int id;
    private String name;
    @Autowired//按照类型
    private xx x;
    public User() {
    }
    public void setX(xx x) {
        this.x = x;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", x=" + x +
                '}';
    }
    public void setId(int id) {
        this.id = id;
    }
    @Value("张三")
    public void setName(String name) {
        this.name = name;
    }

}
//结果:User{id=1, name='张三', x=users.xx@14555e0a}
@Component("xxid")//按照名称
public class xx {
}

	@Autowired
    @Qualifier("xxid")
    private xx x;
//或者Resource(name = "xxid")
// private xx x;

自动装配

两种方式5个属性值
default,byName,byType,constructor,no

XML自动装配

byName根据setter寻找
byType 根据类型找

public class Cat {
}
public class Dog {
}
public class People {
    private Dog dog;
    private Cat cat;
    private int id;

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public void setId(int id) {
        this.id = id;
    }
}
    <bean id="dog" class="users.Dog"></bean>
    <bean id="cat" class="users.Cat"></bean>
    <bean id="people" class="users.People" autowire="byName">
        <property name="id" value="1"></property>
    </bean>

注解自动装配

    <bean id="dog" class="users.Dog"></bean>
    <bean id="cat" class="users.Cat"></bean>

<!-- 注解自动装配-->
    <bean id="people1" class="users.People">
        <property name="id" value="1"></property>
    </bean>
public class People {
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;
    private int id;
    .....}
//可以与 @Qualifier(value = "cat")cat是xml里面的id
    private Cat cat;

下一篇AOP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值