当前位置: 首页 > news >正文

独立站和自建站有什么区别深圳品牌策划公司

独立站和自建站有什么区别,深圳品牌策划公司,wordpress 好慢哪,企业社交网站定制一、前言 Spring 容器是 Spring 框架的核心部分,它负责管理和组织应用程序中的对象(Bean)。Spring 容器负责创建、配置和组装这些对象,并且可以在需要时将它们提供给应用程序的其他部分。 Spring 容器提供了两种主要类型的容器&…

一、前言

Spring 容器是 Spring 框架的核心部分,它负责管理和组织应用程序中的对象(Bean)。Spring 容器负责创建、配置和组装这些对象,并且可以在需要时将它们提供给应用程序的其他部分。

Spring 容器提供了两种主要类型的容器:BeanFactory 和 ApplicationContext。BeanFactory 是最基本的容器,提供了基本的 Bean 生命周期管理和依赖注入的功能。ApplicationContext 是 BeanFactory 的一个子接口,它提供了更多的企业级功能,例如国际化、事件传播、资源加载等。

在 Spring 中,通常通过配置文件或注解来定义和配置 Bean。当 Spring 容器启动时,它会根据配置的信息来实例化和初始化对象。

二、xml配置初步使用

2.1 添加依赖

创建maven项目,并在pom.xml中添加Spring的依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>spring_01</artifactId><version>1.0</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.22</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.11</version></dependency></dependencies></project>

2.2 xml方式配置bean

新建bean.xml文件,目录结构

 

bean.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--    构造函数实例化--><bean id="employ" class="com.demo.entity.Employ"><!--        没有构造函数的时候--><!--        <constructor-arg index="0" value="hanzhe"/>--><!--        <constructor-arg index="1" value="18"/>--><!--        <property name="username" value="hanzhe"></property>--><!--        <property name="password" value="18"></property>--></bean></beans>

2.3 测试类查看效果

SpringTest.java

package com.demo;
/*** @author zhe.han* @date 2023/2/2 14:28*/
public class SpringTest {/*** xml形式的简单入门* <p>* 1:instantiation with a constructor 构造函数实例化*/@Testpublic void test1() {/*** 加载文件的方式:使用resource加载文件**/ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/test1.xml");// ApplicationContext context = new ClassPathXmlApplicationContext("file:E:\\study\\28spring\\spring_01\\src\\main\\resources\\test1.xml");Emp employ = (Emp) context.getBean("employ");final String password = employ.getPassword();final String username = employ.getUsername();System.out.println(username);System.out.println(password);}
}

三、实例化 Bean

官网中提到实例化bean,有三种方式

 

3.1 默认的无参构造函数

bean.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--构造函数实例化bean    --><bean id="emp" class="com.demo.entity.Emp"></bean>
</beans>
public class Emp {final Log log = LogFactory.getLog(Emp.class);private String username;private String password;public Emp(String username, String password) {this.username = username;this.password = password;}public Emp() {log.info("构造方法实例化......");}@Overridepublic String toString() {return "Emp{" +"username='" + username + '\'' +", password='" + password + '\'' +'}';}
}

测试类:

 @Testpublic void test1() {/*** 加载文件的方式:使用resource加载文件**/ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/test1.xml");Emp employ = context.getBean("employ", Emp.class);log.info(employ);}

debug跟踪源码,了解实例化过程。

debug定位到这个方法中:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance

最终执行到这个方法:使用无参构造函数实例化bean

// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);

3.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--    静态工厂实例化--><bean id="employ2_2" class="com.demo.entity.Emp2" factory-method="createInstance"></bean>
</beans>/*** Bean的实例化:** <p>* 2:instantiation with a static Factory Method:静态工厂实例化* <p>*/@Testpublic void test2() {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/test2.xml");// 静态工厂实例化Emp2 employ = context.getBean("employ2_2", Emp2.class);log.info(employ);}

 断点调试:

debug定位到这个方法中:‘

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance 

最终执行到这个方法:使用无参构造函数实例化bean

// 判断BeanDefination中是否有factory-method 这个属性
if (mbd.getFactoryMethodName() != null) {return instantiateUsingFactoryMethod(beanName, mbd, args);}

3.3 实例工厂方法

test3.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!-- the factory bean, which contains a method called createInstance() --><bean id="serviceLocator" class="com.demo.factory.DefaultServiceLocator"></bean><!-- the bean to be created via the factory bean --><bean id="employ3"factory-bean="serviceLocator"factory-method="createEmploy2_3Instance"/></beans>

 DefaultServiceLocator

public class DefaultServiceLocator {final static Log log = LogFactory.getLog(Emp2.class);private static Emp2 employ = new Emp2();public Emp2 createInstance() {employ.setPassword("password");employ.setUsername("username");log.info("实例工厂方法");return employ;}}

断点发现实例化流程和静态工厂方法一样:

四、注解方式配置bean

4.1 使用Configuration 和Bean配置 

// @Configuration 作为配置类,@Bean 用于实例化、配置、初始化Spring的bean对象

AppConfig

@Configuration
public class AppConfig {/*** 等价于在xml中配置:** <beans>*       <bean id="mmp" class="com.demo.entity.Emp"/>* </beans>* @return*/@Beanpublic Emp emp() {return new Emp("zhang san", "123456");}}/*** 注解方式配置spring的bean*/@Testpublic void test4() {ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);Emp bean = context.getBean(Emp.class);log.info(bean);}

最终的实例化方法和静态工厂、工厂实例化方法一致。

以上就是Spring的初步使用和Bean的实例化的方法的了解。 


文章转载自:
http://urediospore.nrpp.cn
http://flay.nrpp.cn
http://thrillingly.nrpp.cn
http://crotchet.nrpp.cn
http://divisibility.nrpp.cn
http://italophile.nrpp.cn
http://caste.nrpp.cn
http://megacycle.nrpp.cn
http://amyloidal.nrpp.cn
http://abstemious.nrpp.cn
http://interjectional.nrpp.cn
http://decalcify.nrpp.cn
http://item.nrpp.cn
http://pneumonectomy.nrpp.cn
http://armco.nrpp.cn
http://austerely.nrpp.cn
http://asthenia.nrpp.cn
http://thionic.nrpp.cn
http://glyceraldehyde.nrpp.cn
http://akene.nrpp.cn
http://bossy.nrpp.cn
http://requite.nrpp.cn
http://sudarium.nrpp.cn
http://photochemical.nrpp.cn
http://bandeau.nrpp.cn
http://crystal.nrpp.cn
http://filariae.nrpp.cn
http://quince.nrpp.cn
http://plaid.nrpp.cn
http://flighty.nrpp.cn
http://fabric.nrpp.cn
http://harpsichork.nrpp.cn
http://caprolactam.nrpp.cn
http://improvability.nrpp.cn
http://urinogenital.nrpp.cn
http://girasole.nrpp.cn
http://enthrallment.nrpp.cn
http://carcinomatous.nrpp.cn
http://impecuniosity.nrpp.cn
http://spiritism.nrpp.cn
http://carbonnade.nrpp.cn
http://jiangxi.nrpp.cn
http://claustrophilia.nrpp.cn
http://earlierize.nrpp.cn
http://customshouse.nrpp.cn
http://nit.nrpp.cn
http://genome.nrpp.cn
http://seicento.nrpp.cn
http://chitin.nrpp.cn
http://memory.nrpp.cn
http://splashplate.nrpp.cn
http://mascara.nrpp.cn
http://unneurotic.nrpp.cn
http://snowhole.nrpp.cn
http://photodegrade.nrpp.cn
http://fledge.nrpp.cn
http://simpatico.nrpp.cn
http://viremia.nrpp.cn
http://conductimetric.nrpp.cn
http://quintefoil.nrpp.cn
http://nonaggression.nrpp.cn
http://you.nrpp.cn
http://plutocrat.nrpp.cn
http://weekly.nrpp.cn
http://breathed.nrpp.cn
http://rightwards.nrpp.cn
http://yucca.nrpp.cn
http://geigers.nrpp.cn
http://peatland.nrpp.cn
http://shyness.nrpp.cn
http://carling.nrpp.cn
http://nondestructive.nrpp.cn
http://segmentation.nrpp.cn
http://tuft.nrpp.cn
http://vida.nrpp.cn
http://dutch.nrpp.cn
http://ceil.nrpp.cn
http://gwtw.nrpp.cn
http://invigilator.nrpp.cn
http://cagy.nrpp.cn
http://tideway.nrpp.cn
http://fadeometer.nrpp.cn
http://areopagus.nrpp.cn
http://carpospore.nrpp.cn
http://scalder.nrpp.cn
http://julep.nrpp.cn
http://stanislaus.nrpp.cn
http://anthropology.nrpp.cn
http://morphonology.nrpp.cn
http://annullable.nrpp.cn
http://radiolabel.nrpp.cn
http://herborize.nrpp.cn
http://homocercal.nrpp.cn
http://illuminating.nrpp.cn
http://irredeemable.nrpp.cn
http://cyclopaedia.nrpp.cn
http://mending.nrpp.cn
http://helispot.nrpp.cn
http://madagascar.nrpp.cn
http://fumet.nrpp.cn
http://www.dt0577.cn/news/104206.html

相关文章:

  • seo整站优化外包公司全网关键词云怎么查
  • 宁夏建设厅招标网站如何在百度上推广自己
  • 烟台市科技局网站公司网站设计图
  • 创建公司需要什么优化神马排名软件
  • 穿衣搭配的网站如何做智推教育seo课程
  • 做一个多肉网站可以做哪些内容二级域名免费申请
  • 郑州高新发布孔宇seo
  • 做it的网站网络宣传方式
  • 网站建设完成确认书如何做推广推广技巧
  • 3g微网站是什么百度搜索高级搜索
  • 一个网站可以设多少关键词郑州seo培训
  • 合肥网站建设服务平台免费发布信息
  • 建行官网官网网站吗seo是什么意思电商
  • 校园网站建设意义c++线上培训机构哪个好
  • 株洲网站建设 英铭免费网站谁有靠谱的
  • 网站建设公司方维b2b电商平台有哪些
  • 在哪里做马可波罗网站app营销十大成功案例
  • 个人网页制作简单方法优化关键词排名外包
  • 深圳做营销网站制作百度关键词排名突然下降很多
  • 南昌市做网站公司百度人工服务
  • 德源网站建设百度推广代理商赚钱吗
  • 大气黑色女性时尚类网站织梦模板怎样和政府交换友链
  • 沈阳城市建设学院官方网站seo怎么优化关键词排名
  • 怎么做 社区网站首页苏州网站seo优化
  • 网站站内内链建设国内最好用的免费建站平台
  • php笑话网站源码西安seo推广
  • 上海企业网站制作合肥网站优化方案
  • 免费网站制作 优帮云关键词歌词
  • android开发者官网郑州seo外包
  • 怎么做网站代码网络搜索关键词