spring的maven依赖Spring的东西
MAVEN的spring依赖
<!-- 1.Spring核⼼依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- 2.Spring dao依赖-->
<!-- spring-jdbc包括了⼀些如jdbcTemplate的⼯具类-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- 3.Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- 4.Spring test依赖:⽅便做单元测试和集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
spring的配置⽂件xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
链接: .
Maven的配置⽂件l打包错误问题(Source option 5 is no longer supported. Use 6 or later.)
**报这个错是因为你的本地开发jdk版本⽐你pom⽂件默认版本⽤的⾼。解决⽅法:在l⽂件中添加如下信息
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version><!--maven-compiler-plugin版本写⼀个中央仓库有的版本,保存后它会⾃动下载 -->
<configuration>
<source>11</source><!--我的jdk是10.0.1的,写成10.0.1不好⽤,所以就写成10了 -->
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
ApplicationContext的创建对象问题
ApplicationContext alc = new ClassPathXmlApplicationContext("l");
User userma = (Bean("userma");
<--getBean只是调⽤User类的构造⽅法不会给他赋属性值除⾮是有参构造-->
userma.saying();
<--调⽤其他⽅法时会直接调⽤set⽅法给其赋值也就是property属性值注⼊-->
神马是DI(依赖注⼊)
**依赖容器注⼊属性**
在⼀个类中直接创建另⼀个类的对象的代码,,我们可以把这种坏味道称为硬初始化(hard init)。同时,我们也应该像记住硬编码⼀样记住,new(对象创建)是有毒的。不太好就。
public class MovieLister {
private MovieFinder finder;
public MovieLister() {
finder = new MovieFinderImpl();
}
}
//这种new对象的就不太好对
public class MovieLister {
private MovieFinder finder;
public MovieLister(MovieFinder finder) {
this.finder = finder;
}
}
//1.构造函数注⼊:这是我认为的最简单的依赖注⼊⽅式,我们修改⼀下上⾯代码中MovieList的构造函数,使得MovieFinderImpl的实现在MovieLister类之外创建。这样,MovieLister就只依赖于我们定义的MovieFinder接⼝,⽽不依赖于MovieFinder的实现了。
public class MovieLister {
public void setFinder(MovieFinder finder) {
this.finder = finder;
resource和autowired注解的区别
}
}
//2setter注⼊:类似的,我们可以增加⼀个setter函数来传⼊创建好的,就是把构造函数换了个名字
public interface InjectFinder {
void injectFinder(MovieFinder finder);
}
class MovieLister implements InjectFinder {
public void injectFinder(MovieFinder finder) {
this.finder = finder;
}
}
//3 接⼝注⼊:接⼝注⼊使⽤接⼝来提供setter⽅法,其实现⽅式如下。⾸先要创建⼀个注⼊使⽤的接⼝。
*最后需要注意的是,依赖注⼊只是控制反转的⼀种实现⽅式。控制反转还有⼀种常见的实现⽅式称为依赖查。
spring标签
<bean id="userma" class="com.kpi.ma.User" name="userma2,userma3">
//id是唯⼀标识符,class是类全名,name是别名可以起很多个空格和逗号都可以分割    <property name="name" value="nerver"/>
</bean>
//别名
<alias name="userma" alias="mama"/>
//整合xml
<import resource="l"/>
<import resource="l"/>
<import resource="l"/>
依赖注⼊bean标签
<bean name="user1" class="com.kpi.ma.User">
</bean>
<bean id="student" class="com.kpi.ma.Student">
<!--⼀般数据类型-->
<property name="name" value="Tom"/>
<!--引⽤类型-->
<property name="user" ref="user1"/>
<!--array数组类型-->
<property name="srr" >
<array>
<value>java</value>
<value>c</value>
<value>c#</value>
<value>c++</value>
</array>
</property>
<!--list集合类型-->
<property name="pants">
<list>
<value>12</value>
<value>12</value>
<value>12</value>
</list>
</property>
<!--map类型-->
<property name="clothers">
<map>
<entry key="12" value="hhh"/>
<entry key="12" value="hhh"/>
<entry key="12" value="hhh"/>
</map>
</property>
<!--set类型-->
<property name="set">
<set>
<value>hi</value>
<value>hi</value>
<value>hi</value>
</set>
</property>
<!--Properties类型-->
<property name="info">
<props>
<prop key="sss"/>
</props>
</property>
</bean>
如何使⽤p-namespace
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:c="/schema/c"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd">
<bean id="myDataSource" class="org.apachemons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="sql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/mydb"
p:username="root"
p:password="misterkaoli"/>
<bean id="myDataSource2" class="org.apachemons.dbcp.BasicDataSource"
destroy-method="close"
c:driverClassName="sql.jdbc.Driver"
c:url="jdbc:mysql://localhost:3306/mydb"
c:username="root"
c:password="misterkaoli"/>
</beans>
//p是属性注⼊c是构造⽅法注⼊
XML配置⽂件中没有注册URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。