在这篇文章中,我们将带领您了解给页面附加的全貌,包括注入js的相关情况。同时,我们还将为您介绍有关AngularJS$injector(依赖注入)、AspNetCore核心通过依赖注入(注入服务)、b
在这篇文章中,我们将带领您了解给页面附加的全貌,包括注入 js的相关情况。同时,我们还将为您介绍有关AngularJS $injector(依赖注入)、AspNet Core 核心 通过依赖注入(注入服务)、bean的装配方式(注入方式,构造注入,setter属性注入)、Boost ASIO-如何在TCP消息前面附加标题的知识,以帮助您更好地理解这个主题。
本文目录一览:- 给页面附加(注入) js(页面中嵌入页面)
- AngularJS $injector(依赖注入)
- AspNet Core 核心 通过依赖注入(注入服务)
- bean的装配方式(注入方式,构造注入,setter属性注入)
- Boost ASIO-如何在TCP消息前面附加标题
给页面附加(注入) js(页面中嵌入页面)
var js = document.createElement("script");
js.type = "text/javascript";
js.src = ''//code.jquery.com/jquery-1.11.2.min.js'' ;
document.body.appendChild(js);
AngularJS $injector(依赖注入)
在AngularJS中也有依赖注入的概念,像spring中的依赖注入,但是又有所不同。Spring中使用构造注入或者设值注入的方式,还需要做一些额外的操作,但是angular中只需要在需要的地方声明一下即可,类似模块的引用,因此十分方便。
参考:[angular api doc] (http://docs.angularjs.cn/api/auto/service/$injector)
推断式注入
这种注入方式,需要在保证参数名称与服务名称相同。如果代码要经过压缩等操作,就会导致注入失败。
app.controller("myCtrl1", function($scope,hello1,hello2){
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
});
标记式注入
这种注入方式,需要设置一个依赖数组,数组内是依赖的服务名字,在函数参数中,可以随意设置参数名称,但是必须保证顺序的一致性。
var myCtrl2 = function($scope,hello1,hello2){
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
}
myCtrl2.$injector = [''hello1'',''hello2''];
app.controller("myCtrl2", myCtrl2);
内联式注入
这种注入方式直接传入两个参数,一个是名字,另一个是一个数组。这个数组的最后一个参数是真正的方法体,其他的都是依赖的目标,但是要保证与方法体的参数顺序一致(与标记注入一样)。
app.controller("myCtrl3",[''$scope'',''hello1'',''hello2'',function($scope,hello1,hello2){
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
}]);
$injector常用的方法
在angular中,可以通过angular.injector()
获得注入器。
var $injector = angular.injector();
通过$injector.get(''serviceName'')
获得依赖的服务名字
$injector.get(''$scope'')
通过$injector.annotate(''xxx'')
获得xxx的所有依赖项
$injector.annotate(xxx)
样例代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl1">
<input type="button" ng-click="hello()" value="ctrl1"></input>
</div>
<div ng-controller="myCtrl2">
<input type="button" ng-click="hello()" value="ctrl2"></input>
</div>
<div ng-controller="myCtrl3">
<input type="button" ng-click="hello()" value="ctrl3"></input>
</div>
<script type="text/javascript">
var app = angular.module("myApp",[]);
app.factory("hello1",function(){
return {
hello:function(){
console.log("hello1 service");
}
}
});
app.factory("hello2",function(){
return {
hello:function(){
console.log("hello2 service");
}
}
});
var $injector = angular.injector();
console.log(angular.equals($injector.get(''$injector''),$injector));//true
console.log(angular.equals($injector.invoke(function($injector) {return $injector;}),$injector));//true
//inferred
// $injector.invoke(function(serviceA){});
app.controller("myCtrl1", function($scope,hello1,hello2){
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
});
//annotated
// function explicit(serviceA) {};
// explicit.$inject = [''serviceA''];
// $injector.invoke(explicit);
var myCtrl2 = function($scope,hello1,hello2){
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
}
myCtrl2.$injector = [''hello1'',''hello2''];
app.controller("myCtrl2", myCtrl2);
//inline
app.controller("myCtrl3",[''$scope'',''hello1'',''hello2'',function($scope,hello1,hello2){
// app.controller("myCtrl3",[''$scope'',''hello1'',''hello2'',function(a,b,c){
// a.hello = function(){
// b.hello();
// c.hello();
// }
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
}]);
console.log($injector.annotate(myCtrl2));//["$scope","hello1","hello2"]
</script>
</body>
</html>
AspNet Core 核心 通过依赖注入(注入服务)
说起依赖注入 相信大家已经很熟悉了,这里我在简要的描述一遍,
什么是依赖注入:
我们从字面意义上来解释一下:依赖代表着两个或者多个对象之间存在某些特定的联系: 举一个不是很恰当的例子
比如说一度夫妻组成的家庭里没有儿女,咱们暂且把这个家庭当做是一个对象(家),但是他们想要领养一个儿女怎么办,他们想通过福利院去找到这个合适的家庭成员,最终家庭里添加了一位新成员,这里福利院的角色咱们就称为注入者,添加的这个成员的动作就称为注入(DI)。 注入方式有很多,举一个简单的代码例子:
这是一个构造器注入的例子
/// <summary>
/// 家庭对象
/// </summary>
public class family
{
/// <summary>
/// 成员
/// </summary>
public string member { get; set; }
//构造函数
public family(string Value )
{
member = Value;
}
}
调用代码 如下
Console.WriteLine("********************构造器注入******************");
family family = new family("儿子");
string ReturnValue= family.member;
Console.WriteLine(ReturnValue);
Console.WriteLine("********************构造器注入******************");
输出结果:
方式有很多种网上资料还是很多的:
回到 AspNetCore 核心的依赖注入:
为什么说起这个:因为在 Core 的 Web 程序启动的过程中,是通过 Core 的启动类 Starup 中注册的 配置 / 和服务 辅助运行。Starup 中配置 / 和服务就是通过依赖注入注入进去的。
注册MVC服务
这是框架自动生成后的
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
自定义自己的服务
public void ConfigureServices(IServiceCollection services)
{
//增加自己的EF服务 并连接 数据库
services.AddEntityFrameworkSqlServer().AddDbContext<EFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
//增加自己的EF 服务用来记录Log
services.AddEntityFrameworkSqlServer().AddDbContext<EFLogDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerLog")));
}
解决方案自动生成的 IOC 容器,返回值为 void 如果想引入第三方容器的话需要更改此类的返回值, 返回值需要更改为 IServiceProvider 此接口只有一个方法
第三方没有用过就不写出来误导人了基本代码如下:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//在这里写第三方容器
}
也可以在这这地方里注入第三方容器
public IActionResult Index()
{
//RequestServices 获取或设置系统。提供对请求的访问的iServiceProvider服务容器。
this.HttpContext.RequestServices.GetService();
return View();
}
bean的装配方式(注入方式,构造注入,setter属性注入)
bean的装配方式有两种,构造注入和setter属性注入。
public class User {
private String username;
private String password;
private List<String> list;
//构造注入需要提供带所有参数的有参构造方法
public User(String username, String password, List<String> list) {
this.username = username;
this.password = password;
this.list = list;
}
//设置注入,需要提供无参构造方法和setter和getter方法
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
public String toString() {
return "User{" +
"username=''" + username + ''\'''' +
", password=''" + password + ''\'''' +
", list=" + list +
''}'';
}
}
在applicationContext.xml中的配置
<!--构造方法方式装配bean-->
<!--在这儿可以采用p名称空间的形式注入值-->
<bean id="user1">
<constructor-arg index="0" value="chen"/>
<constructor-arg index="1" value="123456"/>
<constructor-arg index="2">
<list>
<value>"user1"</value>
<value>"user2"</value>
</list>
</constructor-arg>
</bean>
<!--使用设置注入方式装配bean-->
<!--在这儿可以采用p名称空间的形式注入值-->
<bean id="user2">
<property name="username" value="陈红君"/>
<property name="password" value="145678"/>
<property name="list">
<list>
<value>"4679"</value>
<value>"213"</value>
</list>
</property>
</bean>
<!--例如这儿的bean可以写成这样
<bean id="user2"p:username="小城" p:password="123"/>
-->
</beans>
基于注解的装配
接口UserDao
public interface UserDao {
public void userDaoSay();
}
实现接口的USerDaoImp
@Repository("userDao")//用于数据访问层
public class UserDaoImp implements UserDao {
@Override
public void userDaoSay() {
System.out.println("这是实现userdao的方法");
}
}
接口UserService
public interface UserService {
public void userServiceSay();
}
实现接口的UserServiceImp
@Service("userService")
public class UserServiceImp implements UserService {
@Resource(name = "userDao")//按照实例名称进行装配的
private UserDao userDao;
public void userServiceSay() {
this.userDao.userDaoSay();
System.out.println("userservice to say hello world!");
}
}
控制器UserController
@Controller("userController")//控制器
public class UserController {
@Resource(name = "userService")
private UserService userService;
public void userControllerSay() {
this.userService.userServiceSay();
System.out.println("userController to Say hello world!");
}
/*实例化一个userController对象,调用userControllerSay()方法,userController 先调用userService里的方法
当执行到了userService,userService就去调用执行userDao中的userDaoSay()方法
* */
}
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"
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-3.0.xsd">
<context:component-scan base-package="com.itheima.ioc.zhujiefangshizhuangpeiBean"/>
</beans>
测试
public class annotationTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("anntationContext.xml");
UserController userController = (UserController) applicationContext.getBean("userController");
userController.userControllerSay();
}
}
Boost ASIO-如何在TCP消息前面附加标题
如何解决Boost ASIO-如何在TCP消息前面附加标题?
为便于阅读,此处将我的代码最小化。
我正在遵循本指南:https://www.boost.org/doc/libs/1_63_0/doc/html/boost_asio/example/cpp11/chat/chat_server.cpp
Packet.h
:
#ifndef VIBRANIUM_CORE_PACKET_H
#define VIBRANIUM_CORE_PACKET_H
#include <cstdio>
#include <cstdlib>
#include <cstring>
class Packet {
public:
Packet() : body_length_(0)
{
}
enum { max_body_length = 1024 };
enum { header_length = 8 };
char header_[header_length];
const char* data() const;
char* data();
size_t length() const;
const char* body() const;
char* body();
size_t body_length() const;
void body_length(std::size_t new_length);
bool decode_header();
void encode_header();
private:
char data_[header_length + max_body_length];
size_t body_length_;
};
#endif //VIBRANIUM_CORE_PACKET_H
Packet.cpp
:
#include "Packet.h"
const char *Packet::data() const {
return data_;
}
char *Packet::data() {
return data_;
}
size_t Packet::length() const {
return header_length + body_length_;
}
const char *Packet::body() const {
return data_ + header_length;
}
char *Packet::body() {
return data_ + header_length;
}
size_t Packet::body_length() const {
return body_length_;
}
void Packet::body_length(std::size_t new_length) {
body_length_ = new_length;
if (body_length_ > max_body_length)
body_length_ = max_body_length;
}
bool Packet::decode_header() {
char header[header_length + 1] = "";
std::strncat(header,data_,header_length);
body_length_ = std::atoi(header);
if (body_length_ > max_body_length)
{
body_length_ = 0;
return false;
}
return true;
}
void Packet::encode_header() {
char header[header_length + 1] = "";
std::sprintf(header,"%4d",static_cast<int>(body_length_));
std::memcpy(data_,header,header_length);
}
ServerOpcode.h
:
#ifndef VIBRANIUM_CORE_SERVEROPCODE_H
#define VIBRANIUM_CORE_SERVEROPCODE_H
#include <cstdint>
enum ServerOpcode : uint16_t{
SMSG_AUTH_CONNECTION_RESPONSE = 0x001,SMSG_LOGIN_REQUEST_RESPONSE = 0x002,};
#endif //VIBRANIUM_CORE_SERVEROPCODE_H
因此,我想修改encode_header()
以将ServerOpcode
设置为数据包头。我正在尝试这样做,但是没有用:
void Packet::encode_header(ServerOpcode serverOpcode) {
char header[header_length + 1] = "";
std::strcat(header,static_cast<char>(serverOpcode));
std::memcpy(data_,header_length);
}
我有两个问题:
- 如何使编码标头位于
ServerOpcode
的前面?我的错误在哪里,该如何解决? - 与
decode_header()
相比,我如何解码传入的邮件头?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
关于给页面附加和注入 js的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于AngularJS $injector(依赖注入)、AspNet Core 核心 通过依赖注入(注入服务)、bean的装配方式(注入方式,构造注入,setter属性注入)、Boost ASIO-如何在TCP消息前面附加标题的相关信息,请在本站寻找。
本文标签: