美文网首页
2021-07-20

2021-07-20

作者: 如果大雨落下 | 来源:发表于2021-07-20 17:21 被阅读0次

xml配置文件文件
如果用了xml方式获取了ctx,然后又是用的注解,需要在xml里面增加扫描,扫描对应包下面的注解
<context:component-scan base-package="com.example.Annotion"></context:component-scan>

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        userService.saveUser();

注解,如果用下面的方式获取ctx,不需要去配置扫描了

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.example.Annotion");
        UserService bean = ctx.getBean(UserServiceImpl.class);
        bean.saveUser();

@Autowired
@Qualifier("userDao")
===
@Resource("userDao")
相等的
autowired+Qualifier = resource

加载配置文件

<context:property-placeholder location="classpath:conn.properties"/><!-- 加载配置文件 -->
 
<!-- com.mchange.v2.c3p0.ComboPooledDataSource类在c3p0-0.9.5.1.jar包的com.mchange.v2.c3p0包中 -->
 <bean id="dataSource" class="${dataSource}"> <!-- 这些配置Spring在启动时会去conn.properties中找 -->
    <property name="driverClass" value="${driverClass}" />
    <property name="jdbcUrl" value="${jdbcUrl}" />
    <property name="user" value="${user}" />
    <property name="password" value="${password}" />
 </bean>

集成web

  1. 首先导入坐标
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.16.RELEASE</version>
        </dependency>
  1. 在web.xml中配置监听器, 当监听到servletContext创建的时候就会去加载配置文件.相当于应用一启动就加载了
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  1. 通过WebApplicationContextUtils(spring封装的工具类)来获取WebApplicationContext ,为applicationContext的子类
 ServletContext servletContext = request.getServletContext();
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

相关文章

  • [Microsoft/AI-System]微软AI系统Lab9

    2021-07-20 Lecture12: System for AI-12-Reinforcement lear...

  • [Microsoft/AI-System]微软AI系统Lab8

    2021-07-20 Lecture11: System for AI-11-AutoML systems [ P...

  • 君子和而不同

    日记873篇 2021-07-20 影响人进步,最大的障碍,是被自己的习惯困住。例如总是习惯把精力放在事情上,忽略...

  • “挑戰吧!火星”

    中國載人航天科技成就展 2021-07-20 1.星空之门与航天科普区 打开“星空之门”,踏上宇宙旅程 让你和孩子...

  • #Dairy258 Pause

    2021-07-20 周二 阴 今天像是赌气,也像是放假。赖床到十一点多才起,冲了个澡清醒一下。 这周结束,课就都...

  • 【餐饮100问】50、你有没有一个好的门头?

    D281 2021-07-20 跟西总、史总我们一行人来到大济南溜达,安总介绍了一家门店在转让中,地处芙蓉街,但是...

  • 2021-07-20

    今天上午后来的两位同学把学费交给我了,是孩子的奶奶用微信交给我的,合计壹仟元整,是周殊含与周禹彤的补课费。她...

  • 2021-07-20

    如何论证精彩 之 案例精彩 论点论据精彩其实不是最难的,很多都是套路,套路你熟练之后反倒是更简单的。最难的其实是,...

  • 2021-07-20

    夜跑47分钟,自我感觉效果比跳绳2500个好太多了,全身是汗,体重一下子减轻9两,不敢喝太多水,怕又重回去。 晚上...

  • 2021-07-20

    没事,一切从头开始,

网友评论

      本文标题:2021-07-20

      本文链接:https://www.haomeiwen.com/subject/uddrmltx.html