第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

IDEA下——Spring入門程序

標(biāo)簽:
Spring
  1. 创建一个Maven的项目,我的项目结构如下:

  2. https://img1.sycdn.imooc.com//5bfabd6a0001fd7b02990314.jpg

  3. 在pom文件里写下需要导入的依赖:

  4.    <?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>com.abc</groupId>       <artifactId>01-first(Spring)</artifactId>       <version>1.0-SNAPSHOT</version>       <build>           <plugins>               <plugin>                   <groupId>org.apache.maven.plugins</groupId>                   <artifactId>maven-compiler-plugin</artifactId>                   <configuration>                       <source>6</source>                      <target>6</target>                   </configuration>               </plugin>           </plugins>       </build>       <properties>           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>           <maven.compiler.source>10</maven.compiler.source>           <maven.compiler.target>10</maven.compiler.target>           <spring.version>5.1.0.RELEASE</spring.version>       </properties>       <dependencies>           <dependency>               <groupId>org.springframework</groupId>               <artifactId>spring-beans</artifactId>               <version>${spring.version}</version>           </dependency>           <dependency>               <groupId>org.springframework</groupId>               <artifactId>spring-core</artifactId>               <version>${spring.version}</version>           </dependency>           <dependency>               <groupId>org.springframework</groupId>               <artifactId>spring-context</artifactId>               <version>${spring.version}</version>           </dependency>           <dependency>               <groupId>org.springframework</groupId>               <artifactId>spring-expression</artifactId>               <version>${spring.version}</version>           </dependency>           <dependency>               <groupId>org.springframework</groupId>               <artifactId>spring-jcl</artifactId>               <version>${spring.version}</version>           </dependency>           <dependency>               <groupId>junit</groupId>               <artifactId>junit</artifactId>               <version>4.12</version>               <scope>test</scope>           </dependency>       </dependencies>       <build>           <plugins>               <plugin>                   <artifactId>maven-compiler-plugin</artifactId>                   <configuration>                       <source>1.10</source>                       <target>1.10</target>                   </configuration>               </plugin>           </plugins>       </build>     </project>

  5. 在Java文件夹下新建package,在package包下新建接口及其实现类
    接口:
    public interface SomeService {                void doSome();       }
    实现类:

        public class SomeServiceImpl implements SomeService {    public SomeServiceImpl() {
            System.out.println("创建SomeServiceImpl对象");
        }    @Override
        public void doSome() {
            System.out.println("执行SomeServiceImpl里的doSome()方法");
        }
        }
  6. 在resources目录下新建applicationContext.xml文件

  • 我们需要在spring容器的配置文件中进行注册该Bean

  • spring使用的配置文件为xml文件,当然需要引入约束文件,一般将spring的配置文件命名为applicationContext.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
               http://www.springframework.org/schema/beans/spring-beans.xsd">    
         <!--注册Servcie其造价于如下代码:   SomeServiceImpl someService = new SomeServiceImpl();
                    其底层是通过反射机制创建的someService对象
            Object someService = Class.forName("com.abc.service.SomeServiceImpl").newInstance();-->
                    <bean id="someService" class="com.abc.service.SomeServiceImpl"/>
     </beans>
  • spring的根元素是benas显然是注册Bean,子标签是Bean

  • 注册:<bean id="someService" class="com.abc.service.SomeServiceImpl"/>

  • id属性为了唯一确定一个对象,class属性里边应写类全名

  1. 注册完毕后我们要在测试类中获取spring容器,而获取Spring容器有两种方式。

    package com.abc.service;    import org.junit.Test;    import org.springframework.context.ApplicationContext;    import org.springframework.context.support.ClassPathXmlApplicationContext;    import org.springframework.context.support.FileSystemXmlApplicationContext;    public class SomeServiceImplTTest {         // spring容器获取的两种方式
         @Test
         public void test01(){             //在类路径下加载Spring配置文件
             ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");             //在当前目录的根下加载该配置文件,路径应为文件实际存放的目录
                ApplicationContext ac2 = new FileSystemXmlApplicationContext
                       ("D:\\IDEA-workspace\\01-first(Spring)\\src\\main\\resources\\applicationContext.xml");
                System.out.println(ac2);
          }    
        @Test
        public void test02() {             // 加载Spring配置文件,创建Spring容器对象
             ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");             //调用spring容器的getBean方法获取carImpl,方法参数为bean的id
             SomeService service = (SomeService) ac.getBean("SomeService");
             service.doSome();
            }
        }

Spring入门程序到此就结束了

原文出处:https://www.cnblogs.com/EWEADN/p/10017585.html  

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消