Spring

哈喽Spring

第一:到入spring所需要的maven的依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!--        使用Aop所需要的包-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>

</dependencies>

第二:创建一个Hello的javabean

package com.zzy.pojo;

public class Hello {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                '}';
    }
}

第三:在resources文件夹创建一个application-config.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--    创建一个bean的容器-->
    <!-- 使用spring创建对象   在spring这些都叫做Bean=new 对象-->
    <!-- id="hello" id中的名字是 是读取 这个实体类的标识   -->
    <!--    传统的 Hello hello = new Hello()
                  class=Hello   id = hello
                   property=对象的属性赋值  name是你要赋值的属性  value=你要赋的值                 -->
    <bean id="hello" class="com.zzy.pojo.Hello">
        <property name="name" value="spring"></property>
    </bean>

</beans>

注意:这里是手动注入的一个javabean

第四:编写一个测试类

import com.zzy.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        //获取spring 容器 的上下文对象
         ApplicationContext applicationContext =new ClassPathXmlApplicationContext("beans.xml");
         //我们的所有对象都存在spring中 通过 ApplicationContext applicationContext(获取容器的值) 对象来获取 我们所需要的实体类对象bean
        Hello hello = (Hello) applicationContext.getBean("hello");//他的返回值是object 所以要强转你所需要的对象类型
        System.out.println(hello.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值