首页 > 其他分享 >day118 - 基于xml管理bean的入门案例

day118 - 基于xml管理bean的入门案例

时间:2023-07-13 19:11:35浏览次数:37  
标签:xml spring helloworld bean day118 HelloWorld ioc

基于xml管理bean

入门案例

导入依赖

<dependencies>
    <!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- junit测试 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

 

创建helloworld

package com.gu.spring.pojo;
​
public class HelloWorld {
    public void sayHello(){
        System.out.println("hello spring");
    }
}

 

配置spring

<!--
    bean: 将对象交给ioc容器管理
    属性:
    id:bean得唯一标识,不能重复
    class:设置bean对象所对应的类型
-->
    <bean id="helloworld" class="com.gu.spring.pojo.HelloWorld"></bean>

 

测试类

@Test
public void test(){
    //获取ioc容器
    ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
    //获取ioc容器中的bean对象
    HelloWorld helloworld = (HelloWorld) ioc.getBean("helloworld");
    helloworld.sayHello();
​
}

 

总结

通过测试类中创建ioc的容器对象,读取spring中的配置文件,文件中指定了自定义的组件类,ioc容器调用组件创建对象。

注意

Spring 底层默认通过反射技术调用组件类的无参构造器来创建组件对象,这一点需要注意。如果在需要无参构造器时,没有无参构造器,则会抛出下面的异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name'helloworld' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception isorg.springframework.beans.BeanInstantiationException: Failedto instantiate [com.atguigu.spring.bean.HelloWorld]: No default constructor found; nestedexception is java.lang.NoSuchMethodException:com.atguigu.spring.bean.HelloWorld.()

over

标签:xml,spring,helloworld,bean,day118,HelloWorld,ioc
From: https://www.cnblogs.com/GUGUZIZI/p/17551839.html

相关文章

  • spring bean 的属性为 java.util.Properties 时如何初始化该属性
       publicclassFooBean{privatejava.util.Propertiesattr;publicjava.util.PropertiesgetAttr(){returnattr;}publicvoidsetAttr(java.util.Propertiesattr){this.attr=attr;}} <beanid=......
  • bean加载控制
            ......
  • java 加载bean
    @Service@AutowiredprivateActionLogAspectactionLogAspect;@RestController@RequiredArgsConstructorprivatefinalImComplaintServiceimComplaintService;@Slf4j@RequiredArgsConstructor@ComponentfinalActionLogAspectactionLogAspect;......
  • 《系列二》-- 10、initialize-初始化bean
    目录initializeBean方法源码如下二、重要操作2.1应用Aware2.2applyBeanPostProcessorsBeforeInitialization:2.3invokeInitMethods:2.4applyBeanPostProcessorsAfterInitialization阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主要内容需要看官自......
  • 《系列二》-- 9、bean属性填充
    目录一、概述:populateBean在什么时候执行?二、populateBean的重要操作三、重点操作一propertyValue的注入3.1根据Bean名称注入3.2浅看一下,获取非'简单'类型property的方法3.3根据Bean类型注入四、注入依赖的应用阅读之前要注意的东西:本文就是主打流水账式的源码阅......
  • Bean的实例化
    Bean实例化一.构造方法进行实例化bookDaoImplpackagedang.dao.impl;importdang.dao.BookDao;publicclassBookDaoImplimplementsBookDao{publicBookDaoImpl(){System.out.println("bookdaoconstructorisrunning....");}publicv......
  • 若依微服务使用openfeign ,写了一个接口,但是其他项目引入的时候显示找不到这个Bean:Coul
    启动报错:org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname'tokenController':Unsatisfieddependencyexpressedthroughfield'sysLoginService';nestedexceptionisorg.springframework.beans.fa......
  • service 无法注入bean问题
    Noqualifyingbeanoftype'com.unqd.api.weituo.service.IamCustomerService'available:expectedatleast1beanwhichqualifiesasautowirecandidate.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true......
  • python解析xml
    主要是查询标签:importxml.dom.minidoms='''xml字符串''''''这里做一些解释:<?xmlversion="1.0"encoding="UTF-8"?><soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/......
  • Visual C# 中XML注释换行
    只需将<para>标记用于诸如<summary>、<remarks>或<returns>等标记内即可 ///<summary>///基类(第1行)///<para>说明:(第2行)</para>///<para>封装一些常用的成员(第3行)</para>///<para>前面要用全角空格才能显示出空格来(第4行)</para>///</s......