1.使用构造注入完成属性赋值
(1)创建实体Greeting,构造一个带参数构造器
public class Greeting {
//说话的人
private String person;
//说话的内容
private String words;
public Greeting() {
}
public Greeting(String person, String words) {
this.person = person;
this.words = words;
}
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
/*
* 定义说话的方法
* */
public void sayGreeting(){
System.out.println(person+"说:“"+words+"”");
}
}
(2)编写beans.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">
<!--使用有参构造函数创建对象 根据参数位置传值-->
<bean id="ikun" class="pojo.Greeting" >
<constructor-arg index="0" value="ikun"/>
<constructor-arg index="1" value="我是练习时长2年半的练习生,我喜欢花式敲代码,我会唱跳rap篮球敲代码!"/>
</bean>
<!-- 根据参数名称传值-->
<bean id="rod" class="pojo.Greeting" >
<constructor-arg name="person" value="Rod" />
<constructor-arg name="words" value="世界上有10中人,认识二进制的和不认识二进制的。" />
</bean>
</beans>
(3)测试用例
@Test
public void pNameTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Greeting ikun= (Greeting) context.getBean("ikun");
ikun.sayGreeting();
Greeting rod = (Greeting) context.getBean("rod");
rod.sayGreeting();
}
2.使用p命名空间注入直接量
(1)创建beans.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="zhangGa" class="pojo.Greeting" p:person="嘎子" p:words="三天不打小鬼子,手都痒痒!"/>
<bean id="rod" class="pojo.Greeting" p:person="Rod" p:words="世界上有10中人,认识二进制的和不认识二进制的。"/>
<bean id="rodc" class="pojo.Greeting" c:_0="Rod" c:_1="世界上有10中人,认识二进制的和不认识二进制的。"/>
</beans>
(2)测试用例
@Test
public void pNameTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("pbeans.xml");
Greeting zhangGa= (Greeting) context.getBean("zhangGa");
zhangGa.sayGreeting();
Greeting rod = (Greeting) context.getBean("rod");
rod.sayGreeting();
}
注意,关于p,c命名注入是必须在beans.xml文件的beans上加以上内容表示引用

以下是关于我学习p,c命名空间是的注入的链接
(4条消息) spring——Spring Bean属性注入——短命名空间注入——p 命名空间注入(setter注入)..._小白龙白龙马的博客-CSDN博客 (4条消息) spring 使用p命名空间和c命名空间进行依赖注入_使用p命名空间注入直接量_而活的博客-CSDN博客
3.使用p命名空间注入Bean组件
(1)创建实体类User
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
(2)创建DAO包,创建UserDao接口,里面有save方法
public interface UserDao {
void save(User user);
}
实现UseDao接口UserDaoImpl类
public class UserDaoImpl implements UserDao{
@Override
public void save(User user) {
System.out.println(user.toString());
System.out.println("保存用户信息导数据库");
}
}
(3)创建Service包,并创建UserService接口
public interface UserService {
void addUser(User user);
}
实现UserService接口UserServiceImpl
public class UserServiceImpl implements UserService{
private UserDao dao;
public UserServiceImpl(UserDao dao) {
this.dao = dao;
}
public UserServiceImpl() {
}
@Override
public void addUser(User user) {
dao.save(user);
}
}
(4)创建beans.xm文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="pojo.User" p:name="ikun" p:age="25"/>
<bean id="userDao" class="DAO.UserDaoImpl"/>
<bean id="userService" class="Service.UserServiceImpl">
<constructor-arg>
<ref bean="userDao" />
</constructor-arg>
</bean>
</beans>
(5)测试
@Test
public void addUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
UserServiceImpl userService = (UserServiceImpl) context.getBean("userService");
userService.addUser(user);
}
4.使用注解实现依赖注入
(1)编写UserDao接口及其实现类UserDaoImpl,使用恰当的注解将实现类标注为Bean组件。
public interface UserDao {
void save(User user);
}
import org.springframework.stereotype.Component;
import pojo.User;
@Component("userDao")
public class UserDaoImpl implements UserDao{
@Override
public void save(User user) {
System.out.println(user.toString());
System.out.println("保存用户信息导数据库");
}
}
(2)编写业务接口UserService及其实现类UserServiceImpl,使用恰当的注解将实现类标注为Bean组件。
public interface UserService {
void addUser(User user);
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import DAO.UserDao;
import pojo.User;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
@Qualifier("userDao")
private UserDao dao;
public UserServiceImpl(UserDao dao) {
this.dao = dao;
}
public UserServiceImpl() {
}
@Override
public void addUser(User user) {
dao.save(user);
}
}
(3 )使用注解为业务Bean注入所依赖的Dao组件
(4 )编写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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--注入类参数-->
<!-- <bean id="user" class="pojo.User" p:name="ikun" p:age="25"/>-->
<!--
使用注解方式进行创建对象
1.开启注解扫描
含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包与子包中所有的类
查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象,
放到spring容器中
-->
<context:component-scan base-package="Service,DAO,pojo"/>
</beans>
(5)编写测试代码,运行代码以检验效果。
@Test
public void getUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
// User user=new User("ikun",25);
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userService");
userServiceImpl.addUser(user);
}
由于这里要传入一个实例user,所以这里有很多方法,在beans.xml文件中声明一个user,传入参数, 也可以使用注解@Value传入参数
public class User {
@Value("ikun")
private String name;
@Value("25")
private int age;
//get,set,构造器,toString()方法...
}
注意,在创建beans.xml文件,引用context:component-scan时需要添加下面的内容

否则会出现以下错误,-3.2可以去掉,
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 16 in XML document from class path resource [beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 16; columnNumber: 62; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。
学习链接
(4条消息) Spring 使用注解实现依赖注入_spring注释依赖_小伙阿k的博客-CSDN博客
5.使用.properties文件加载配置注入集合类型的属性
(1)编写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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="personList" class="pojo.person">
<property name="hobbies">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>篮球</value>
</list>
</property>
</bean>
<bean id="personSet" class="pojo.person">
<property name="hobbies">
<set>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>篮球</value>
</set>
</property>
</bean>
<bean id="personMap" class="pojo.person">
<property name="hobbies">
<map>
<entry>
<key><value>sing</value></key>
<value>唱</value>
</entry>
<entry>
<key><value>jump</value></key>
<value>跳</value>
</entry>
<entry>
<key><value>Rap</value></key>
<value>rap</value>
</entry>
<entry>
<key><value>basketball</value></key>
<value>篮球</value>
</entry>
</map>
</property>
</bean>
<bean id="personProperties" class="pojo.person">
<property name="hobbies">
<props>
<prop key="sing">唱</prop>
<prop key="jump">跳</prop>
<prop key="Rap">Rap</prop>
<prop key="basketBall">篮球</prop>
</props>
</property>
</bean>
</beans>
(2)实体类person
public class person {
private List hobbies;
public List getHobbies() {
return hobbies;
}
public void setHobbies(List hobbies) {
this.hobbies = hobbies;
}
}
(3)测试
@Test
public void updateUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
person userList = (person) context.getBean("personList");
System.out.println(userList.getHobbies().toString());
person userSet = (person) context.getBean("personSet");
System.out.println(userSet.getHobbies().toString());
person userMap = (person) context.getBean("personMap");
System.out.println(userMap.getHobbies().toString());
person personProperties = (person) context.getBean("personProperties");
System.out.println(personProperties.getHobbies().toString());
}
4224

被折叠的 条评论
为什么被折叠?



