关于Spring程序

1. Spring程序

1.1 helloSpring

  1. 配置xml文档Bean.xml
    这里的 id=“hello” class=“com.wu.pojo.Hello” 就相当于创建一个对象
    Hello hello = new Hello(); id是变量名,class是变量类型。
    ~~ ~~
    property标签内是在为str变量设置值,相当于实体类内的set方法(如果实体类
    中的setStr方法被删除,那么这句就会报错。)
<?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">

    <bean id="hello" class="com.wu.pojo.Hello">
        <property name="str" value="hello"/>
    </bean>
</beans>
  1. 编写一个实体类:
public class Hello {
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "hello{" +
                "str='" + str + '\'' +
                '}';
    }
}
  1. 测试:

org.springframework.context.ApplicationContext: 接口代表 Spring IoC 容器,并负责实例化,配置和组装 Bean。容器通过读取配置元数据来获取有关要实例化,配置和组装哪些对象的指令。配置元数据以 XML,Java 注解或 Java 代码表示。它使您能够表达组成应用程序的对象以及这些对象之间的丰富相互依赖关系。

new ClassPathXmlApplicationContext():用来加载配置文件
通过 getBean()来获得配置文件中的bean以达到获取对象的目的。

public class Mytest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
        Hello hello = (Hello)context.getBean("hello");
        System.out.println(hello.toString());
    }
}

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

    <bean id="userdao" class="com.wu.dao.userDaoImpl"/>
    <bean id="mysqldao" class="com.wu.dao.mysqlDaoImpl"/>
    <bean id="userservice" class="com.wu.service.userServiceImpl">
        <property name="userDao" ref="userdao"/>
    </bean>
</beans>

ref:表示对象,value:表示值(通过ref的值来改变选择的实体类)
property标签内对应的是:

    private userDao userDao;
    public void setUserDao(com.wu.dao.userDao userDao) {
        this.userDao = userDao;
    }

测试:

    @Test
    public void test2() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Bean.xml");
        userServiceImpl userservice = (userServiceImpl) applicationContext.getBean("userservice");
        userservice.getuser();
    }

这样我们可以通过改变Bean.xml内的配置来选择使用哪个实现类。

2. IOC创建对象方式

创建一个类的对象就是在初始化此类的构造方法,当我们写一个有参的构造方法时(此时系统不会再隐式的创建空参的构造方法),会发现在Bean.xml中会报错。
在这里插入图片描述
所以构造方法是IOC创建对象方式的关键。

2.1 有参构造方法的Bean文件写法

  1. 通过寻找下标
    <bean id="user" class="com.wu.pojo.user">
        <constructor-arg index="0" value="lll"/>
    </bean>
  1. 通过参数类型
    <bean id="user" class="com.wu.pojo.user">
        <constructor-arg type="java.lang.String" value="lll"/>
    </bean>
  1. 直接通过变量名设置
    <bean id="user" class="com.wu.pojo.user">
        <constructor-arg name="name" value="lll"/>
    </bean>

注:同时不管是否会被调用,在获得容器(ApplicationContext context)的同时Bean内设置的对象都会被创建,所有的对象都是被加载好了的。

3. Spring配置说明

就是beans标签内的子标签性质:

  1. alias 别名 :
    <bean id="student" class="com.wu.pojo.student">
        <property name="name" value="1"/>
    </bean>
    <alias name="student" alias="student2"/>

注:虽然起了别名但是原来的名字还是可以继续用。

  1. bean:
    id:标识单个 bean 定义的字符串,是唯一标识。
    class:定义 bean 的类型并使用完全限定的类名。
    name:与id类似,同时也是别名,但它可以起多个别名,不同的别名之间用分割符隔开。

  2. import:
    在多人合作的项目中会用到,创建一个ApplicationContext.xml,使用import将大家的Bean文件导入,这样就可以使用不同的bean了。

    <import resource="Bean.xml" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值