1. spring的配置文件怎麼寫
標準的Spring配置文件編寫:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- 配置數據源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>
jdbc:mysql://localhost/ssh?characterEncoding=utf-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>
<!--配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/ssh/pojo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 事務管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- hibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置數據持久層 -->
<bean id="userDao"
class="com.ssh..impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 配置業務邏輯層 -->
<bean id="userService"
class="com.ssh.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 配置控制層 -->
<bean id="UserAction"
class="com.ssh.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<!-- 配置pojo -->
<bean id="User" class="com.ssh.pojo.User" scope="prototype"/>
</beans>
2. 在MyEclipse里怎樣創建Spring的配置文件
方法如下:
1、打開Myeclipse,找到新建的工程項目;
2、右鍵點擊--Myeclipse--project facets--install spring facet,按圖示找到那片小綠葉;
3. spring配置文件的名字
來看一個標準的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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
4. springcloud-config client配置文件為什麼要用bootstrap命名
這是由spring boot的載入屬性文件的優先順序決定的,你想要在載入屬性之前去spring cloud config server上取配置文件,那spring cloud config相關配置就是需要最先載入的,而bootstrap.properties的載入是先於application.properties的,所以config client要配置config的相關配置就只能寫到bootstrap.properties里了
5. Spring的配置文件必須叫applicationContext.xml或applicationContext.xml-xxx嗎
當然不需要,你可以在web.xml中指定任何你想要的名稱甚至路徑
6. springcloud-config client配置文件為什麼要用bootstrap命名
項目,升鎮世需要訪問多個資料庫,而且需旅早要在伺服器運行不重新啟動的情況下,動態的修改spring中配置的數據源datasource,在網上找了很多資料,最後找到了適合我的方法,下面總結一下。 spring的配置吵肢文件是在容器啟動的時候就載入到內存中的,如果手動...
7. spring讀取配置文件加入命名空間時為什麼要加一個命名空間前綴
APP_CODE 寫的類 在同一WEB項目的手亮代碼中使用不需要額譽友外引用。
namespace N
{
public Class XXX{}
}
在default.cs中只需要慶薯槐 N.XXX x=new N.XXX 即可。
8. Spring有幾種配置方式
Spring有幾種配置方式 ?
基於XML文件的配置 這種配置文件的格式常用<beans>開頭,然後運用一系列的bean定義和專門的應用配置選項組成。 Spring XML配置方式是使用被Spring命名空間所支持的一些列XML的標簽來實現的。
基於註解的配置 可以使用註解的方式來代替XML方式的bean元素的配置。這就是組件掃描,常用依賴注入的一些註解有: @Controller @Service @Autowired @RequestMapping @RequestParam @ModelAttribute @Cacheable @CacheFlush @Resource @PostConstruct @PreDestroy @Repository @Scope @SessionAttributes @InitBinder @Required @Qualifier
組件掃描: 容器會掃描base-package指定的包及其子包下面的所有類,如果該類有一些特定的註解,則納入容器進行管理。
在類前面添加的一些特定的註解: @Component 通用註解 @Repository 持久層註解 @Service 業務層註解、 @Controller 控制層註解
基於Java的配置
9. SpringBoot的配置文件有哪幾種格式
SpringBoot中的配置文件來主要有三種格式,自properties、yaml、和xml方式。
- 其中properties格式配置文件後綴是.properties,配置項為:server.port = 9090
- yaml格式配置文件後綴是.yml,配置項是:server.port: 9090
在SpringBoot中,使用最廣泛的配置文件是yaml,yaml之所以流行,除了他配置語法精簡之外,還因為yaml是一個跨編程語言的配置文件。
在SpringBoot中,除了yaml之外,properties也比較常用,但是XML幾乎不用,看得出來Spring團隊非常痛恨XML配置文件!認為它不是一個好的語言。
如果你對常見的配置文件有哪幾種格式不熟悉,就去黑馬程序員官網視頻庫看免費視頻。
10. spring的xml配置文件的xml文件頭詳解
在spring的xml配置文件中,在頭部會出現如下的東西
這些奇怪的xmlns和很長的url的作用是什麼呢?
首先,介紹一下 xmlns 的作用,如下所示,一個 xml 文檔中如果包含如下兩種定義不同, 但是名稱相同的元素, xml 解析器是無法解析的, 因為它不能確定當你調用document.getElementsByTagName("book") 時應該返回哪個元素。
這時候可以通過在名稱增加前綴解決這個問題
由此,引入一個概念 命名空間 ,通過增加前綴表示不同的那是不同命名空間下的table,從而解決了矛盾,但是不同的人都有自己創建的不同的命名空間來描述同樣的東西,不利於xml文件信息的解析,比如說,同樣都是水果,可以從顏色和香味不同角度來定義成如下兩種形式:
為此,w3c(萬維網聯盟)對於一些類型,定義了對應的命名空間和這些類型的標准,xml解釋器碰到這些類型的時候就會通過這些標准去解析這類型的標簽,為了確保命名空間的唯一,所以不同的命名空間的通常使用URL作為被識別的id,如下例子:
這句話的作用是當前引入了一個叫做xsi的命名空間,xsi可以在接下來要使用該命名空間時所使用的,如下:
而 http://www.w3.org/2001/XMLSchema-instance 這個很長的字元串,則是xsi這個名稱空間被xml解釋器內部所識別的時候所真正使用的id,但也本身只是被當做一個字元串名字去處理,xml解釋器根據這個id去獲取它對應的標准,從而知道這個命名空間定義有什麼樣的標簽(xml解釋器自帶有一些通用的命名空間的標准),這個字元串雖然看起來是URL,但是和對應的網頁上的信息沒有關系,只是用來提供命名空間 唯一性 的作用,網址有時可以被打開,上面會有關於該命名空間的信息。
所以,spring配置文件中這三句話分別表示,引入了三個命名空間。
其中第一個xmlns後面沒有空間名的,表示引入了一個默認的名稱空間,下文中不使用命名空間前綴的都默認使用這個命名空間,這個默認的命名空間,其真正的id是 " http://www.springframework.org/schema/beans " 。
引入的第二個命名空間叫做xsi,其真正的id是 " http://www.w3.org/2001/XMLSchema-instance "
引入的第三個命名空間叫做context,其真正的id是 " http://www.springframework.org/schema/context "
在最後可以看到xsi:schemaLocation,這句話的意思表示使用命名空間xsi下的schemaLocatioin,設置了它對應的值為後面很多很多的URL,schemaLocation中存儲的值每兩個為一組, 第一個代表命名空間,第二個代表該命名空間的標準的文件位置 ,如下所示,這句話就是說明命名空間 http://www.springframework.org/schema/beans 的標准文件是 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd *
因為xml解釋器不一定含有所有命名空間的標准,通過這樣設置就可以告訴xml解釋器不同命名空間的對應的標準是什麼了,而這也是xsi這個命名空間的作用,要用到其schemaLocation。
最後,對應一般的xml解釋器的工作流程中,xml解釋器識別到有 「 http://www.w3.org/2001/XMLSchema-instance " 這個通用的名稱空間後,明白知道要引入一些不同命名空間,就會從其schemaLocation中獲取不同命名空間和其對應的標准。