32.Spring-对象依赖.md

时间:2023-03-08 17:10:39
32.Spring-对象依赖.md

目录

1.对象依赖的分类

Spring中,给对象属性赋值的方法:

  • 构造函数
  • Set方法
  • p命名空间
  • 自动装配
  • 注解

1.1构造函数

构造方法通过配置文件中constructor-arg标签实现,使用时候需要注意要有带参的构造函数

1.2.Set方法

通过配置文件中property标签实现,使用时候需要注意要有无参构造方法和Getter Setter函数

1.3p命名空间

通过配置文件中p:XXX-ref=""标签实现,这里的引用也可换成使用实际值。

Spring 3.0以上版本支持

1.4自动装配

分为两种方式:

byName:会自动在对应类中寻找注入类名称一致的对象注入

byType:会自动在对应类中寻找注入类同类型的对象自动注入,但是要注意的是,一个类中只能有一个类型,不同同类型!

这两种配置方式可以全局配置,配置位置:

http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byType">

简化开发,但是不利于后期维护,不推荐使用。

1.5注解

注解的的好出是简化配置文件的维护

1.5.1关键字

通过注解完成依赖和注入:

  • @Repository:对应持久层
  • @Service:对应业务层
  • @Controller:对应控制层
  • @Resource:表示要注入的对象
  • 配置文件中,通过开启扫描告知Spring使用注解

1.5.2注意事项

  • @Component做为通用关键字可以替代@Repository、@Service和@Controller,但是不建议在对应类中这么写,因为不利于理解和维护。
  • 对应的注解关键字可以不写,Spring会自动根据需要的类按照类名首字母小写的方式自动查找。但是这样同样不利于开发维护,不建议。

2.实例

对于构造函数和Set方法,使用User类示例。p命名空间模拟开发过程

2.1先创建各对象,注意需要的构造和Getter Setter方法

package per.liyue.springlearing.structure;

/**
* Created by liyue on 2016/11/10.
*/
public class User
{
private int userId;
private String userName; @Override
public String toString()
{
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
'}';
} public User()
{
} public User(int userId, String userName)
{
this.userId = userId;
this.userName = userName;
} public int getUserId()
{
return userId;
} public void setUserId(int userId)
{
this.userId = userId;
} public String getUserName()
{
return userName;
} public void setUserName(String userName)
{
this.userName = userName;
}
}
package per.liyue.springlearing.structure;

/**
* Created by liyue on 2016/11/10.
*/
public class UserDao
{
public void Save()
{
System.out.println("执行了Save方法保存数据");
}
}
package per.liyue.springlearing.structure;

/**
* Created by liyue on 2016/11/10.
*/
public class UserService
{
public UserDao getUserDao()
{
return userDao;
} public void setUserDao(UserDao userDao)
{
this.userDao = userDao;
} private UserDao userDao; public void Save()
{
userDao.Save();
} }
package per.liyue.springlearing.structure;

/**
* Created by liyue on 2016/11/10.
*/
public class UserAction
{
private UserService userService; public String execute()
{
userService.Save();
return null;
} public UserService getUserService()
{
return userService;
} public void setUserService(UserService userService)
{
this.userService = userService;
}
}

2.2配置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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" > <!--1.构造函数方法
注意要有对应的带参构造方法
-->
<bean id="userC" class="per.liyue.springlearing.structure.User" scope="prototype">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="张三"></constructor-arg>
</bean> <!--2.Set方法
注意要有无参构造方法和Getter Setter函数
-->
<bean id="userS" class="per.liyue.springlearing.structure.User" scope="prototype">
<property name="userId" value="2"></property>
<property name="userName" value="李四"></property>
</bean> <!--3.p命名空间
注意要有Getter Setter函数
-->
<!--
<bean id="userDao" class="per.liyue.springlearing.structure.UserDao"></bean> <bean id="userService" class="per.liyue.springlearing.structure.UserService" p:userDao-ref="userDao"></bean> <bean id="userAction" class="per.liyue.springlearing.structure.UserAction" p:userService-ref="userService"></bean>
--> <!--4.自动装配
分为两种方式:
byName:会自动在对应类中寻找注入类名称一致的对象注入
byType:会自动在对应类中寻找注入类同类型的对象自动注入,但是要注意的是,一个类中只能有一个类型,不同同类型!
这两种配置方式可以全局配置,配置位置:
http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byType">
-->
<bean id="userDao" class="per.liyue.springlearing.structure.UserDao"></bean> <bean id="userService" class="per.liyue.springlearing.structure.UserService" autowire="byName"></bean> <bean id="userAction" class="per.liyue.springlearing.structure.UserAction" autowire="byType"></bean> </beans>

2.3模拟调用

package per.liyue.springlearing.structure;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by liyue on 2016/11/10.
*/
public class App
{
private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("per/liyue/springlearing/bean.xml"); @Test
public void Main()
{
User user1 = applicationContext.getBean("userC", User.class);
System.out.println("1.构造方法方式为对象属性赋值" + user1); User user2 = applicationContext.getBean("userS", User.class);
System.out.println("2.Set方法方式为对象属性赋值" + user2); UserAction userAction = applicationContext.getBean("userAction", UserAction.class);
System.out.println("3.p命名空间方法执行:");
userAction.execute(); }
}

2.4输出

1.构造方法方式为对象属性赋值User{userId=1, userName='张三'}

2.Set方法方式为对象属性赋值User{userId=2, userName='李四'}

3.p命名空间方法执行:

执行了Save方法保存数据

2.5注解方式

将上述的类修改:

2.5.1持久层

package per.liyue.springlearing.obj_dep;

import org.springframework.stereotype.Repository;

@Repository(value = "userDao")
public class UserDao
{
public void Save()
{
System.out.println("操作数据保存了数据");
}
}

2.5.2业务层

package per.liyue.springlearing.obj_dep;

import org.springframework.stereotype.Service;
import per.liyue.springlearing.structure.UserDao1; import javax.annotation.Resource; @Service(value = "userService")//这里等于配置文件中<bean id="userDao" class="per.liyue.springlearing.structure.UserDao"></bean>
//的方式
public class UserService
{
@Resource(name = "userDao")
private UserDao1 userDao; public void Save()
{
userDao.Save();
} }

2.5.3控制层

package per.liyue.springlearing.obj_dep;

import org.springframework.stereotype.Controller;
import per.liyue.springlearing.structure.UserService1; import javax.annotation.Resource; @Controller(value = "userAction")
public class UserAction
{
@Resource(name = "userService")
private UserService1 userService; public void Excute()
{
userService.Save();
}
}

2.5.4配置文件

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" > <context:component-scan base-package="per.liyue.springlearing.obj_dep"></context:component-scan>
</beans>

2.5.5调用

package per.liyue.springlearing.obj_dep;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App_Dep
{
private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("per/liyue/springlearing/obj_dep/bean.xml"); @Test
public void Main()
{
UserAction userAction = applicationContext.getBean("userAction", UserAction.class);
System.out.println("使用注解生成:");
userAction.Excute();
}
}