3.Hello Spring(第一个Spring项目)

3.Hello Spring

报错cannot resolve method getBean(java.lang.String)

添加注解@Autowired

public classMyTest {
    @Autowired
public static voidmain(String[] args) {
        ApplicationContext context =newClassPathXmlApplicationContext("beans.xml");
//我们的对象现在都在spring中的管理了,我们要使用,直接去里面取出来就可以了!
Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());//hello.tostring().sout;
}
}
<!--使用spring;来创建对象,再spring这些都称为bean
 java表示方法
 类型 变量名 = new类型{};
 Hello hello = new hello();
 bean表示方法
 bean =对象 new hello();
 id =变量名
 class = new的对象;
 property相当于给对象中的属性设置一个值;
-->
<bean id="hello"class="com.itxiaofei.pojo.Hello">
    <property name="str" value="spirng">
    </property>
</bean>

//获取ApplicationContext:拿到spring容器

ApplicationContext:是spring继BeanFactory之外的另一个核心接口或容器,允许容器通过应用程序上下文环境创建、获取、管理bean。为应用程序提供配置的中央接口。在应用程序运行时这是只读的,但如果实现支持这一点,则可以重新加载。

ApplicationContext context =new ClassPathXmlApplicationContext("beans.xml");

在父模块中导入jar包

<dependencies>
        <!-- <https://mvnrepository.com/artifact/org.springframework/spring-webmvc> -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <!--导入junit4.12-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

pojo的Hello.java

public class Hello {
    public String str;

    public String getStr() {
        return str;
    }

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

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

在resource里面的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>">
    <!-- 使用spring;来创建对象,再spring这些都称为bean
     java表示方法
     类型 变量名 = new 类型{};
     Hello hello = new hello();
     bean表示方法
     bean = 对象 new hello();
     id = 变量名
     class = new 的对象;
     property 相当于给对象中的属性设置一个值;
     -->
    <bean  id="hello"  class="com.itxiaofei.pojo.Hello">
        <property name="str" value="spirng">
        </property>
    </bean>

</beans>

测试类MyTest

public class MyTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象现在都在spring中的管理了,我们要使用,直接去里面取出来就可以了!
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());//hello.tostring().sout;
    }
}
//Hello类public void setStr(String str) {		this.str = str;}

思考:

 

 

IOC:对象由Spring 来创建,管理,装配!

弹幕评论里面的理解:

原来这套程序是:你写好菜单买好菜,客人来了自己把菜炒好招待,就相当于你请人吃饭现在这套程序是:你告诉楼下餐厅,你要哪些菜,客人来的时候,餐厅把做好的你需要的菜送上来IoC:炒菜这件事,不再由你自己来做,而是委托给了第三方__餐厅来做

此时的区别就是,如果我还需要做其他的菜,我不需要自己搞菜谱买材料再做好,而是告诉餐厅,我要什么菜,什么时候要,你做好送来

.

在前面第一个module试试引入Spring

<?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 id="userDaomSql" class="dao.UserDaoMysqlImpl"></bean>
	 <bean id="userServiceImpl" class="service.UserServiceImp">        
		<!--ref引用spring中已经创建很好的对象-->       
		<!--value是一个具体的值,基本数据类型-->        
		<property name="userDao" ref="userDaomSql"/>  
  </bean>
</beans>

第一个module改良后测试


public class MyTest0 {
	public static void main(String[] args) {	
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");	
		UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");		
		userServiceImpl.getUser();
	}
}

总结:

所有的类都要装配的beans.xml 里面;

所有的bean 都要通过容器去取;

容器里面取得的bean,拿出来就是一个对象,用对象调用方法即可;

posted @ 2022-10-28 08:45  It小飞呀  阅读(52)  评论(1编辑  收藏  举报