Spring简单配置及简单IOC

spring可以帮我们创建对象

更改依赖关系时,不需要改代码,直接更改配置文件

对象的数量,更好控制

beans配置文件

<?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">
</beans>

IOC

控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.

例子:

接口:

public interface UserDao {
    void savc();
}

实现类:

public class MysqlImpl implements UserDao{
    @Override
    public void savc() {
        System.out.println("mysql");
    }
}
public class OrangerImpl implements UserDao{
    @Override
    public void savc() {
        System.out.println("oranger");
    }
}
<!--在spring里创建对象-->

<bean id="orang" class="app.mapper.OrangerImpl"/>
<bean id="mysql" class="app.mapper.MysqlImpl"/>
<bean class="app.service.UserServiceImpl" id="service">
    <property name="userDao" ref="orang"/>
</bean>

测试:

springIOC容器代表性接口ApplicationContext

ApplicationContext常见的基于xml的实现类为:
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext.

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-config.xml");//加载配置文件
Computer bean = context.getBean("computer", Computer.class);//使用getBean获取对象

别名

方法一:

<bean class="app.service.UserServiceImpl" id="service">
    <property name="userDao" ref="orang"/>
</bean>
<!--设置别名:在获取Bean的时候可以使用别名获取-->
<!--name 对应上面的id alias别名-->
    <alias name="service" alias="sev"></alias>

方法二:使用name属性

<bean class="app.service.UserServiceImpl" id="service" name="sc sv,st;ss">
    <property name="userDao" ref="orang"/>
</bean>

导入多个配置文件中的配置

1.创建ApplicationContext对象时传入多个配置文件

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-config.xml");//加载配置文件
Computer bean = context.getBean("computer", Computer.class);//使用getBean获取对象

2.用import标签导入(整合配置文件)

<import resource="beans1.xml"/>
 <import resource="beans2.xml"/>

注意:id与name的区别

  1. id是bean的唯一标识符号,若没有Id那么name为默认标识符号
  2. 如果配置了id又配置了name,那么name为别名,别名可以配置多个,这些别名用逗号、空格等隔开。
  3. 还可以通过标签配置别名,name传入需要配置的bean的id或者name,alias为别名,同样可以是多个,用逗号或者空格隔开。
  4. 如果不给bean配置id和name只配置了class,那么可以通过ApplicationContext.getBean(Class)函数获取bean.

三种实例化对象的方式

实体类:

//计算机类
public class Computer {
    private  String name;
    //构造方法
	public Computer(){
    	System.out.println("使用构造方法创建了对象");
	}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

//计算机工厂
public class ComputerFactory {
    //静态方法
    public static Computer computerfcOne(){
        Computer computer=new Computer();
        computer.setName("使用工长创建对象");
        return computer;
    }
	//非静态方法
    public Computer computerTow(){
        Computer computer=new Computer();
        computer.setName("使用工长创建里联想对象");
        return computer;
    }
}

1.使用构造方法创建

实体类里必须要有构造方法

<bean id="comp" class="app.Computer"/>

特性:每次都是创建了一个class对象

2.用静态工厂方法实例化

<bean id="coppy" class="app.ComputerFactory" factory-method="computerfcOne"/>

特性:工厂里的方法必须是静态的,factory-method是调用工厂里的静态方法的

3.使用实例工厂方法实例化

<bean id="cooTow" class="app.ComputerFactory"/>
    <bean id="computer" factory-bean="cooTow" factory-method="computerTow"/>

特性:工厂里的方法是非静态的,需要使用到factory-bean属性和factory-method属性

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值