一、掃描順序
1、項目先讀取web.xml文件,根據如下配置讀取applicationcontext.xml文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>
2、applicationcontext.xml文件內配置情況
<!-- 讀取properties文件內信息,在java中使用@Value(${})註解引入,在xml中使用${}引入使用 -->
<bean id="propertyConfigurer1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<context:annotation-config/>
<!-- 自動掃描bean,不掃描controller,交給springmvc掃描 -->
<context:component-scan base-package="com.secoo.pis">
<!-- 掃描帶有@Controller註解的類。因爲這些類已經隨容器啓動時,在servlet-context中掃描過 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入其他xml文件 -->
<import resource="./datasource.xml"/>
<!-- 自動掃描 將Mapper接口生成代理注入到Spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>
3、讀取web.xml文件,根據如下配置讀取到springmvc-servlet.xml文件
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
4、掃描帶controller註解的bean
<context:component-scan base-package="com.secoo.pis" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
二、spring和springmvc掃描註解的關係
答:spring和springmvc是父子容器,spring作爲父容器優先加載,掃描service層和dao層,不能訪問子容器的bean;springmvc作爲子容器在spring容器加載完成之後再加載,掃描controller層,能夠訪問父容器的bean。所以controller能調service和dao,但service和dao不能調controller。springmvc不掃描service和dao的原因是事務作爲springAOP的特性,配置在spring文件中,springmvc掃描service和dao則事務失效。springmvc掃描controller是因爲控制controller跳轉的配置寫在springmvc配置文件中,springmvc不掃描controller則訪問url不能正常跳轉。
ps:無論是spring還是springmvc只會掃描有註解的bean
三、爲什麼能區分conf-dev、conf-test、conf-pro文件夾
答:在pom.xml配置了profile屬性
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<ver_type>-DEV-SNAPSHOT</ver_type>
</properties>
<build>
<resources>
<resource>
<directory>${project.basedir}/conf_dev/</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>test</id>
<properties>
<ver_type>-SNAPSHOT</ver_type>
</properties>
<build>
<resources>
<resource>
<directory>${project.basedir}/conf_test/</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>pro</id>
<properties>
<ver_type>.RELEASE</ver_type>
</properties>
<build>
<resources>
<resource>
<directory>${project.basedir}/conf_deploy/</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
四、爲什麼自動注入要注入接口而不是實現類?
答:開發中會對實現類做增強,如事務,日誌等,實現增強的AOP技術是通過動態代理實現的,而spring默認是JDK動態代理,對實現類對象做增強得到的增強類與實現類是兄弟關係,所以不能用實現類接收增強類對象,只能用接口接收。想要注入實現類則必須將代理模式改爲Cglib代理
<aop:aspectj-autoproxy proxy-target-class="true"/>
ps:若是一個普通bean沒有接口,則可以直接注入
五、BeanFactory和FactoryBean的區別
答:兩者無任何關係
BeanFactory,bean的工廠類,是Ioc容器的核心接口,爲Ioc容器確立了規範。他的子接口有ApplicationContext,實現類ClassPathXmlApplicationContext。
FactoryBean,工廠bean,區別於普通bean